-1

I want to replace all the GD code with imagick code from the "cropping jquery plugin" from www.croppic.net

Here is the original file https://github.com/sconsult/croppic/blob/master/img_crop_to_file.php

what I have done

    // 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');

The result is an black image with an incorrect width and height. Hope somebody can help me. Thanks in advance!

4
  • 4
    This isn't a code writing service - why not narrow it down to a single thing that is going wrong, rather than asking people to just debug your code sscce.org
    – Danack
    Aug 5, 2015 at 15:43
  • 1
    I have to agree with Danack -- there's too much going on here. Can you please narrow the question down to the piece of code that's having problems?
    – Ben Claar
    Aug 7, 2015 at 18:45
  • 4
    So, you'll spend some reputation on trying to get someone to answer the question, but not any effort to reduce the question to a bit of code that fits on a screen. Also, you ought to post an example input and output image.
    – Danack
    Aug 8, 2015 at 11:54
  • take a look at github.com/avalanche123/Imagine Aug 8, 2015 at 22:30

2 Answers 2

4
+100

The problem is in line with resizeImage method. Please pay attention to 3rd and 4th params: int $filter , float $blur. You can find more details here

Try to replace this line with the following to get a proper result:

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

You can play with a demo with different filters here

0
1

This is the new image width and height.

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

follow these steps

 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;
            }

and at final step

$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);

this is the code worked on me :) try and play

1
  • actually i have done this a long time ago and it takes me a crucial time to solve it.i have the same issue and and i corrected it with this code :)
    – Arun
    Aug 18, 2015 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.