使用图像复制重采样 + 裁剪裁剪图像


Crop Image using imagecopyresampled + Jcrop

我正在使用Jcrop来裁剪用户的图像。我按照这里的教程进行操作:http://deepliquid.com/content/Jcrop_Implementation_Theory.html。这是我的代码

private function resizeImage($file_info)
{
    $dst_w        = $dst_h        = 150;
    $jpeg_quality = 90;
    $src                  = realpath($file_info['directory'] . '/' . $file_info['name']);
    // CHMOD before cropping
    chmod($src, 0777);
    switch (strtolower($file_info['mime'])) {
        case 'image/jpeg':
            $src_img = imagecreatefromjpeg($src);
            break;
        case 'image/png':
            $src_img = imagecreatefrompng($src);
            break;
        case 'image/gif':
            $src_img = imagecreatefromgif($src);
            break;
        default:
            exit('Unsupported type: ' . $file_info['mime']);
    }
    $dst_img = imagecreatetruecolor($dst_w, $dst_h);
    // Resize and crop the image
    imagecopyresampled(
        $dst_img, $src_img,                 // Destination and Source image link resource
        0, 0,                               // Coordinate of destination point
        $this->crop['x'], $this->crop['y'], // Coordinate of source point
        $dst_w, $dst_h,                     // Destination width and height
        $this->crop['w'], $this->crop['h']  // Source width and height
    );
    // Output the image
    switch (strtolower($file_info['mime'])) {
        case 'image/jpeg':
            imagejpeg($dst_img, null, $jpeg_quality);
            break;
        case 'image/png':
            imagepng($dst_img);
            break;
        case 'image/gif':
            imagegif($dst_img);
            break;
        default:
            exit('Unsupported type: ' . $file_info['mime']);
    }
}

我已经检查了目录和文件都是可写的(0777权限)。我使用的所有变量也都定义得很好,没有任何错误。但是,最终图像仍保持其原始形式,未裁剪。

有人知道为什么它不起作用吗?任何帮助,不胜感激。

您使用list()来获取源图像的宽度和高度,但未在imagecopyresampled()调用中使用它。据我所知,其他一切看起来都很好,如果这不能解决它,问题可能出在课堂上的其他地方。

这是我的裁剪方法供参考,如果有帮助的话。

    /**
     * Crops the image to the given dimensions, at the given starting point
     * defaults to the top left
     * @param type $width
     * @param type $height
     * @param type $x
     * @param type $y
     */
    public function crop($new_width, $new_height, $x = 0, $y = 0){
        $img = imagecrop($this->image, array(
            "x" => $x, 
            "y" => $y,
            "width" => $new_width,
            "height" => $new_height
        ));
        $this->height = $new_height;
        $this->width = $new_width;
        if(false !== $img) $this->image = $img;
        else self::Error("Could not crop image.");
        return $this;
    }


调整大小的方法,供参考..

/**
 * resizes the image to the dimensions provided
 * @param type $width (of canvas)
 * @param type $height (of canvas)
 */
public function resize($width, $height){
    // Resize the image
    $thumb = imagecreatetruecolor($width, $height);
    imagealphablending($thumb, false);
    imagesavealpha($thumb, true);
    imagecopyresampled($thumb, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
    $this->image = $thumb;
    $this->width = $width;
    $this->height = $height;
    return $this;
}