php高精細度非破壊画像圧縮機能の実現コード
アップロードされた大きな画像を圧縮し、特に体積、微信などのAPPの応用にもよく使われますが、デフォルトでは圧縮があります。では、どのように画像を大幅に圧縮しても高精細度を保つことができますか?
圧縮は通常、比例してスケーリングされ、幅を指定して圧縮されています。効果はとてもいいです。デジタルカメラで撮った4 Mの写真は圧縮後、より高い解像度と原図の幅の高い値を維持しました。700 Kしかありません。
コードは次の通りです。
comppress.php
締め括りをつける
以上述べたのはPHP高精細度写真圧縮機能の実現コードです。皆さんに助けてほしいです。もし何か質問があれば、メッセージをください。編集はすぐに皆さんに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。
圧縮は通常、比例してスケーリングされ、幅を指定して圧縮されています。効果はとてもいいです。デジタルカメラで撮った4 Mの写真は圧縮後、より高い解像度と原図の幅の高い値を維持しました。700 Kしかありません。
コードは次の通りです。
comppress.php
<?php
require_once 'imgcompress.class.php';
$source = 'test.png';//
$dst_img = 'test_.png';//
$percent = ; # , ,
$image = (new imgcompress($source,$percent))->compressImg($dst_img);
imgcomppress.class.php
<?php
/**
* : 。
* , $percent 。
* , 。 M 。 KB 。 , 。
*
* : 、 。
*/
class imgcompress{
private $src;
private $image;
private $imageinfo;
private $percent = .;
/**
*
* @param $src
* @param float $percent
*/
public function __construct($src, $percent=)
{
$this->src = $src;
$this->percent = $percent;
}
/**
* @param string $saveName ( , ) 。
*/
public function compressImg($saveName='')
{
$this->_openImage();
if(!empty($saveName)) $this->_saveImage($saveName); //
else $this->_showImage();
}
/**
* :
*/
private function _openImage()
{
list($width, $height, $type, $attr) = getimagesize($this->src);
$this->imageinfo = array(
'width'=>$width,
'height'=>$height,
'type'=>image_type_to_extension($type,false),
'attr'=>$attr
);
$fun = "imagecreatefrom".$this->imageinfo['type'];
$this->image = $fun($this->src);
$this->_thumpImage();
}
/**
* :
*/
private function _thumpImage()
{
$new_width = $this->imageinfo['width'] * $this->percent;
$new_height = $this->imageinfo['height'] * $this->percent;
$image_thump = imagecreatetruecolor($new_width,$new_height);
// , ,
imagecopyresampled($image_thump,$this->image,,,,,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
imagedestroy($this->image);
$this->image = $image_thump;
}
/**
* : saveImage()
*/
private function _showImage()
{
header('Content-Type: image/'.$this->imageinfo['type']);
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image);
}
/**
* :
* @param string $dstImgName 、 , 。、 。
*/
private function _saveImage($dstImgName)
{
if(empty($dstImgName)) return false;
$allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif']; // , ,
$dstExt = strrchr($dstImgName ,".");
$sourseExt = strrchr($this->src ,".");
if(!empty($dstExt)) $dstExt =strtolower($dstExt);
if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);
//
if(!empty($dstExt) && in_array($dstExt,$allowImgs)){
$dstName = $dstImgName;
}elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs)){
$dstName = $dstImgName.$sourseExt;
}else{
$dstName = $dstImgName.$this->imageinfo['type'];
}
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image,$dstName);
}
/**
*
*/
public function __destruct(){
imagedestroy($this->image);
}
}
使用後の個人的な感覚では、0.5ぐらいに設定すればいいです。圧縮された画像は原図の品質とほぼ同じです。締め括りをつける
以上述べたのはPHP高精細度写真圧縮機能の実現コードです。皆さんに助けてほしいです。もし何か質問があれば、メッセージをください。編集はすぐに皆さんに返事します。ここでも私たちのサイトを応援してくれてありがとうございます。