使用PHP调整.PNG图像大小

时间:2016-12-16 12:35:28

标签: 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();
    $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的调整大小?

2 个答案:

答案 0 :(得分:0)

您必须关闭Alpha混合,并保存原始Alpha通道数据。在imagecopyresampled()电话后执行此操作:

imagealphablending($new_image, false);
imagesavealpha($new_image, true);

答案 1 :(得分:0)

$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);