简体   繁体   English

使用 PHP 调整 .PNG 图像的大小

[英]Resize .PNG images using PHP

I upload image with mime type png, and everything fine, file have transparent background我上传了 mime 类型 png 的图片,一切正常,文件有透明背景

but begin I need change resize for this file like this但开始我需要像这样改变这个文件的大小

/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;
}

and then I save my file然后我保存我的文件

/**
 * @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);
    }
}

and after resize I have black backgrounf but I don't need this, how to change resize for png mimetype ??调整大小后,我有黑色背景,但我不需要这个,如何更改 png mimetype 的调整大小?

You have to turn Off alpha blending, and save the original alpha channel data.您必须关闭 alpha 混合,并保存原始 alpha 通道数据。 Do this after the imagecopyresampled() call:imagecopyresampled()调用之后执行此操作:

imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$imageKind = array( 'image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png'); $WIDTH = 600; $HEIGHT = 400; $type = $_FILES['photo_' . $i]['type']; if (in_array($type, $imageKind)) { $image = $_FILES['photo_' . $i]['tmp_name']; switch ($type) { case 'image/JPG': case 'image/pjpeg': case 'image/jpeg': $source = imagecreatefromjpeg($image); $extension = '.jpg'; break; case 'image/X-PNG': case 'image/PNG' : case 'image/png': case 'image/x-png': $source = imagecreatefrompng($image); $extension = '.png'; break; } $time = explode(" ", microtime()); $time_name = ($time[1] + $time[0]) * 10000; $filename = ip2long($IP) . "_" . $time_name . "_" . $i . $extension; array_push($filename_array, $filename); array_push($fields, "photo_" . $i); array_push($values, $filename); list($width, $height) = getimagesize($image); $resize = imagecreatetruecolor($WIDTH, $HEIGHT); imagecopyresampled($resize, $source, 0, 0, 0, 0, $WIDTH, $HEIGHT, $width, $height); switch ($type) { case 'image/JPG': case 'image/pjpeg': case 'image/jpeg': imagejpeg($resize, $image); break; case 'image/X-PNG': case 'image/PNG' : case 'image/png': case 'image/x-png': imagealphablending($resize, false); imagesavealpha($resize, true); imagepng($resize, $image); break; } move_uploaded_file($image, "../upload/" . $filename);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM