【发布时间】:2016-12-16 12:35:28
【问题描述】:
我上传了 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();
$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 的调整大小?
【问题讨论】:
-
不确定您使用的是什么库,但请查看:stackoverflow.com/questions/279236/… 或此:stackoverflow.com/questions/13596794/…
标签: php png image-resizing