【问题标题】:Crop and Resize Image - Replace GD with imagick裁剪和调整图像大小 - 用 imagick 替换 GD
【发布时间】:2015-10-27 21:18:15
【问题描述】:

我想用 imagick 代码替换所有 GD 代码 来自www.croppic.net的“裁剪jquery插件”

这是原始文件 https://github.com/sconsult/croppic/blob/master/img_crop_to_file.php

我做了什么

    // original sizes
    $imgInitW               = $_POST['imgInitW'];
    $imgInitH               = $_POST['imgInitH'];

    // resized sizes
    $imgW                   = $_POST['imgW'];
    $imgH                   = $_POST['imgH'];

    // offsets
    $imgY1                  = $_POST['imgY1'];
    $imgX1                  = $_POST['imgX1'];

    // crop box
    $cropW                  = $_POST['cropW'];
    $cropH                  = $_POST['cropH'];

    // rotation angle
    $angle                  = $_POST['rotation'];

    //quali
    $jpeg_quality           = 100;

/*  resize the original image to size of editor */
//  $imgUrl = '/kunden/40/0_org.jpg';
//  $img_r = imagecreatefromjpeg($imgUrl);
//  $source_image = imagecreatefromjpeg($imgUrl);
//  $type = '.jpeg';
//  $resizedImage = imagecreatetruecolor($imgW, $imgH);
//  imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);    
    $im = new imagick('/kunden/40/0_org.jpg');
    $im->resizeImage($imgW, $imgH, 0, 0, false);        

/*  rotate the rezized image */
//  $rotated_image = imagerotate($resizedImage, -$angle, 0);
    $im->rotateimage('#fff', -$angle);

/*  find new width & height of rotated image */
//  $rotated_width = imagesx($rotated_image);
//  $rotated_height = imagesy($rotated_image);
    $d = $im->getImageGeometry();
    $rotated_width = $d['width'];
    $rotated_height = $d['height'];

/*  diff between rotated & original sizes */
    $dx = $rotated_width - $imgW;
    $dy = $rotated_height - $imgH;

/*  crop rotated image to fit into original rezized rectangle */
//  $cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
//  imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
//  imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
    $im->cropImage($imgW,$imgH, $dx/2, $dy/2); // i think this is not correct

/*  crop image into selected area */
//  $final_image = imagecreatetruecolor($cropW, $cropH);
//  imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
//  imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);

/*  save image */
//  imagejpeg($final_image, $output_filename.'.jpg', $jpeg_quality);
    $im->writeImage('/kunden/40/0.jpg');

结果是一个宽度和高度不正确的黑色图像。 希望有人能帮助我。提前致谢!

【问题讨论】:

  • 这不是一个代码编写服务——为什么不把它缩小到一个出错的地方,而不是要求人们只调试你的代码sscce.org
  • 我必须同意 Danack 的观点——这里发生了太多事情。您能否将问题缩小到有问题的代码段?
  • 因此,您将花费一些声誉来试图让某人回答问题,但不会花费任何努力将问题简化为适合屏幕的代码。此外,您应该发布一个示例输入和输出图像。

标签: php gd crop image-resizing imagick


【解决方案1】:

问题与 resizeImage 方法一致。 请注意第三和第四个参数: int $filter , float $blur 。您可以找到更多详情here

尝试将此行替换为以下内容以获得正确的结果:

$im->resizeImage($imgW, $imgH, imagick::FILTER_LANCZOS, 1, false);

你可以玩一个带有不同过滤器的演示here

【讨论】:

    【解决方案2】:

    这是新的图片宽度和高度。

    $new_width = $max_width = $img_width = $image->getImageWidth();
    $new_height = $max_height = $img_height = $image->getImageHeight();
    

    按照这些步骤操作

     if (($img_width / $img_height) >= ($max_width / $max_height)) {
                    $new_width = 0; // Enables proportional scaling based on max_height
                    $x = ($img_width / ($img_height / $max_height) - $max_width) / 2;
                } else {
                    $new_height = 0; // Enables proportional scaling based on max_width
                    $y = ($img_height / ($img_width / $max_width) - $max_height) / 2;
                }
    

    最后一步

    $success = $image->resizeImage(
                $new_width,
                $new_height,
                isset($options['filter']) ? $options['filter'] : imagick::FILTER_LANCZOS,
                isset($options['blur']) ? $options['blur'] : 1,
                $new_width && $new_height // fit image into constraints if not to be cropped
            );
            if ($success && $crop) {
                $success = $image->cropImage(
                    $max_width,
                    $max_height,
                    $x,
                    $y
                );
                if ($success) {
                    $success = $image->setImagePage($max_width, $max_height, 0, 0);
                }
            }
            $type = strtolower(substr(strrchr($file_name, '.'), 1));
            switch ($type) {
                case 'jpg':
                case 'jpeg':
                    if (!empty($options['jpeg_quality'])) {
                        $image->setImageCompression(Imagick::COMPRESSION_JPEG);
                        $image->setImageCompressionQuality($options['jpeg_quality']);
                    }
                    break;
            }
            if (!empty($options['strip'])) {
                $image->stripImage();
            }
            return $success && $image->writeImage($new_file_path);
    

    这是在我身上工作的代码:) 试试玩

    【讨论】:

    • 实际上我很久以前就这样做了,我花了很长时间来解决它。我有同样的问题,我用这段代码纠正了它:)
    猜你喜欢
    • 2010-11-03
    • 2012-12-27
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多