【问题标题】:Crop Image using imagecopyresampled + Jcrop使用 imagecopyresampled + Jcrop 裁剪图像
【发布时间】:2016-05-21 04:43:07
【问题描述】:

我正在使用 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 权限)。我使用的所有变量也都定义明确,没有错误。但是,最终的图像仍然是原始形式,没有被裁剪。

有人知道为什么它不起作用吗?任何帮助表示赞赏。

【问题讨论】:

    标签: php image crop jcrop


    【解决方案1】:

    您正在使用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;
    }
    

    【讨论】:

    • 是的,我忘记删除 list() 行了,呵呵。感谢您对 imagecrop 的参考,虽然 PHP 手册中尚未记录,但我一定会尝试
    • 哦,imagecrop 也可以调整大小吗?我正在使用 imagecopyresampled 裁剪图像并将其大小调整为 150x150
    • 没有裁剪不会调整大小,但如果有帮助,我也添加了我的调整大小方法。
    猜你喜欢
    • 2011-06-25
    • 2012-05-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2014-05-22
    • 1970-01-01
    相关资源
    最近更新 更多