调整大小/裁剪图像并添加空白区域

时间:2013-03-11 15:01:12

标签: php function gd image-resizing

我有调整大小和裁剪功能:

$file = imagecreatefromjpeg($imagefile["tmp_name"]);

    if($imagefile["type"] == "image/jpg" || $imagefile["type"] == "image/jpeg") {
        $uploaded_file = "/var/www/adimages/".$fid."-".$imagefile["name"];
     } elseif($imagefile["type"] == "image/png") {
        $uploaded_file = "/var/www/adimages/".$fid."-".$imagefile["name"];
     }

    imagejpeg($file, $uploaded_file, 100);
    chmod($uploaded_file, 0644);

    list($width, $height, $type, $attr) = getimagesize($uploaded_file);
    $this->imageSizeW = $width; 
    $this->imageSizeH = $height;
    $this->newSizeW = 250;
    $this->newSizeH = 400;

    $aspect = $width / $height;
    $new_aspect = $this->newSizeW / $this->newSizeH;

    if($aspect >= $new_aspect) {
         $new_height = $this->newSizeH;
         $new_width = $width / ($height / $this->newSizeH);
     } else {
         $new_width = $this->newSizeW;
         $new_height = $height / ($width / $this->newSizeW);
     }

     $img_source = imagecreatefromjpeg($uploaded_file);
     $newimage = imagecreatetruecolor($this->newSizeW, $this->newSizeH);
     imagecopyresampled($newimage, $img_source, 0 - ($new_width - $this->newSizeW) / 2, 0 - ($new_height - $this->newSizeH), 0, 0, $new_width, $new_height, $width, $height);
     imagejpeg($newimage, $uploaded_file, 100);            
}

几乎可以正常工作 - 当它裁剪时它会移除部分图像。 我可以以某种方式扩展/配置此功能,以便它将调整图像大小/居中,并添加空白,使图像原始,但仍然是250 x 400 px

1 个答案:

答案 0 :(得分:0)

使用来自voret.net的Class upload 0.32 - 这就是我想要的!