使用PHP调整.PNG图像的大小
我上传的图像与mime类型png,一切都很好,文件有透明的背景 但是开始我需要像这样改变这个文件的大小使用PHP调整.PNG图像的大小,php,png,image-resizing,Php,Png,Image Resizing,我上传的图像与mime类型png,一切都很好,文件有透明的背景 但是开始我需要像这样改变这个文件的大小 /home/ivan/host/olegroom/app/../web/uploads/photo/20161216022845_png-001.png /** * @param string $width */ public function resizeToWidth($width) { $ratio = $width / $this->getWidth();
/home/ivan/host/olegroom/app/../web/uploads/photo/20161216022845_png-001.png
/**
* @param string $width
*/
public function resizeToWidth($width)
{
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width, $height);
}
/**
* @param string $width
* @param string $height
* @param int $left
* @param int $top
*/
public function resize($width, $height, $left = 0, $top = 0)
{
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, $left, $top, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
然后我保存我的文件
/**
* @param string $filename
* @param int $image_type
* @param int $compression
* @param null $permissions
*/
public function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 95, $permissions = null)
{
if ($image_type == IMAGETYPE_JPEG) {
imagejpeg($this->image, $filename, $compression);
} elseif ($image_type == IMAGETYPE_GIF) {
imagegif($this->image, $filename);
} elseif ($image_type == IMAGETYPE_PNG) {
imagepng($this->image, $filename);
}
if ($permissions != null) {
chmod($filename, $permissions);
}
}
调整大小后,我有黑色背景,但我不需要这个,如何更改png mimetype的大小 必须关闭alpha混合,并保存原始alpha通道数据。在调用
imagecopyresampled()
后执行此操作:
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$imageKind=array(
'图像/pjpeg',
“图像/jpeg”,
‘image/JPG’,
“图像/X-PNG”,
“图像/PNG”,
“图像/png”,
“图像/x-png”);
$WIDTH=600;
$HEIGHT=400;
$type=$\u文件['photo.'$i]['type'];
if(在数组中($type,$imageKind)){
$image=$\u文件['photo\u'.$i]['tmp\u name'];
交换机($类型){
案例“image/JPG”:
案例“image/pjpeg”:
案例“图像/jpeg”:
$source=imagecreatefromjpeg($image);
$extension='.jpg';
打破
案例“图像/X-PNG”:
案例“image/PNG”:
案例“image/png”:
案例“图像/x-png”:
$source=imagecreatefrompng($image);
$extension='.png';
打破
}
$time=explode(“,microtime());
$time_name=($time[1]+$time[0])*10000;
$filename=ip2long($IP)。“..$time\u name.”“..$i.$extension;
array\u push($filename\u array,$filename);
阵列推送($fields,“photo_uuu”。$i);
数组\u push($value,$filename);
列表($width,$height)=getimagesize($image);
$resize=ImageCreateTureColor($WIDTH,$HEIGHT);
imagecopyresampled($resize,$source,0,0,0,$WIDTH,$HEIGHT,$WIDTH,$HEIGHT);
交换机($类型){
案例“image/JPG”:
案例“image/pjpeg”:
案例“图像/jpeg”:
imagejpeg($resize,$image);
打破
案例“图像/X-PNG”:
案例“image/PNG”:
案例“image/png”:
案例“图像/x-png”:
imagealphablending($resize,false);
imagesavealpha($resize,true);
imagepng($resize,$image);
打破
}
移动上传的文件($image,“../upload/”$filename);
不确定您使用的是什么库,但请查看此:或此: