Search code examples
phpphp-gd

Creating thumbnail and resizing problems


I'm trying to create thumbnail and resize image at the same time, so to be more clear here is the image that i'm trying to crop:

enter image description here

And i would like to cut out that red area.

Now my problem is that i'm resizing my image with html before croping so when i submit data to php i get incorrect values, like y = 100 when realy it could be y = 200 so i need to find a way to calculate my values.

I am using imagecopyresampled, maybe there is something better then this command?

Also my closest soliution was this:

imagecopyresampled(
    $thumb, //Destination image link resource.
    $src,   //Source image link resource.
    0,      //x-coordinate of destination point.
    0,      //y-coordinate of destination point.
    0,      //x-coordinate of source point.
    0,      //y-coordinate of source point.
    120,    //Destination width.
    160,    //Destination height.
    $image_width/2, //Source width.
    $image_height/2  //Source height.
);

In this case it would cut out left corner but size would be not the same as my red box. So i guess i need to get source width and source height right and everything else should fit perfectly, anyways i hope i make any sense here :)

EDIT Sorry i forgot to mention, $image_width and $image_height is the original image size

EDIT 2 To be more clear this is what i get when i resize with this code

$dimensions = getimagesize('testas.jpg');

$img = imagecreatetruecolor(120, 160); 
$src = imagecreatefromjpeg('testas.jpg');

imagecopyresampled($img, $src, 0, 0, 0, 0, 120, 160, $dimensions[0]/2, $dimensions[1]/2); 

imagejpeg($img, 'test.jpg');

enter image description here

Resized image size is correct, but as you can it doesn't look right.


Solution

  • I use something like this to scale images:

    public static function scaleProportional($img_w,$img_h,$max=50)
    {
        $w = 0;
        $h = 0;
    
        $img_w > $img_h ? $w = $img_w / $img_h : $w = 1;
        $img_h > $img_w ? $h = $img_h / $img_w : $h = 1;
    
        $ws = $w > $h ? $ws = ($w / $w) * $max : $ws = (1 / $h) * $max;
        $hs = $h > $w ? $hs = ($h / $h) * $max : $hs = (1 / $w) * $max;
    
        return array(
            'width'=>$ws,
            'height'=>$hs
        );
    }
    

    usage:

                $getScale = Common::scaleProportional($prevWidth,$prevHeight,$scaleArray[$i][1]);
                $targ_w = $getScale['width'];
                $targ_h = $getScale['height'];
                $jpeg_quality = 100;
    
                $src = $prevdest;
    
                $img_r = imagecreatefromjpeg($src);
                $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
                //imagecopyresampled(dest_img,src_img,dst_x,dst_y,src_x,src_y,dst_w,dst_h,src_w,src_h);
                imagecopyresampled($dst_r,$img_r,0,0,0,0,$targ_w,$targ_h,$prevWidth,$prevHeight);
                imagejpeg($dst_r, 'assets/images/'.$scaleArray[$i][0].'/'.$filename, $jpeg_quality);
                $complete[] = $scaleArray[$i][0];