【问题标题】:Resize .PNG images using PHP使用 PHP 调整 .PNG 图像的大小
【发布时间】: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 的调整大小?

【问题讨论】:

标签: php png image-resizing


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    $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);
    

    【讨论】:

      猜你喜欢
      • 2012-11-15
      • 2011-07-01
      • 1970-01-01
      • 2011-09-28
      • 2014-04-14
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多