PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 11, 2022

[FIXED] Why is the rotated image the same aspect ratio?

 October 11, 2022     gd, php     No comments   

Issue

I'm using some PHP code from jquery file upload and I'm trying to rotate an image prior to saving it. Below is my function call:

public function CreateThumb($file_name, $options){

    $file_path = $options['src_dir'].$file_name;
    $new_file_path = $options['dst_dir'].$file_name;

    list($img_width, $img_height) = @getimagesize($file_path);

    if (!$img_width || !$img_height) {
        return false;
    }
    $scale = min(
            $options['max_width'] / $img_width,
            $options['max_height'] / $img_height
    );

    $new_width = $img_width * $scale;
    $new_height = $img_height * $scale;
    $new_img = @imagecreatetruecolor($new_width, $new_height);

    switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
        case 'jpg':
        case 'jpeg':
            $src_img = @imagecreatefromjpeg($file_path);
            $write_image = 'imagejpeg';
            $image_quality = isset($options['jpeg_quality']) ?
            $options['jpeg_quality'] : 95;
            break;
        case 'gif':
            @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
            $src_img = @imagecreatefromgif($file_path);
            $write_image = 'imagegif';
            $image_quality = null;
            break;
        case 'png':
            @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
            @imagealphablending($new_img, false);
            @imagesavealpha($new_img, true);
            $src_img = @imagecreatefrompng($file_path);
            $write_image = 'imagepng';
            $image_quality = isset($options['png_quality']) ?
            $options['png_quality'] : 9;
            break;
        default:
            $src_img = null;
    }

    $src_img = imagerotate($src_img, 90, 0) ;

    $success = $src_img && @imagecopyresampled(
            $new_img,
            $src_img,
            0, 0, 0, 0,
            $new_width,
            $new_height,
            $img_width,
            $img_height
    ) && $write_image($new_img, $new_file_path, $image_quality);

    // Free up memory (imagedestroy does not delete files):
    @imagedestroy($src_img);
    @imagedestroy($new_img);

    return $success;
}

The image gets rotated but it still maintains it's original aspect ratio and is cropping the photo. Any idea what I'm doing wrong?


Solution

Your problem is in how you set the new values:

$new_width = $img_width * $scale;
$new_height = $img_height * $scale;

Should be, in case of a 90 degrees rotation:

$new_width = $img_height * $scale;    // reverse height and width
$new_height = $img_width * $scale;    // reverse height and width

Edit: And as the original image is rotated, the old width and height have to be reversed:

$success = $src_img && @imagecopyresampled(
        $new_img,
        $src_img,
        0, 0, 0, 0,
        $new_width,
        $new_height,
        $img_height,    // reverse width and height
        $img_width      // reverse width and height
) && $write_image($new_img, $new_file_path, $image_quality);


Answered By - jeroen
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing