Hi,
how can I achieve sth like this in PHP GD (resize image, but canvas still has x,y size the same and image is centered):

from this

to this

Thanks a lot in advance.

    I'm no expert in GD, but here's what I'd do:

    1. Make a thumbnail out of the original image (resize it, actually). (lots of tutorials out there)
    2. Create a new image with white background (or w\e) and the original image dimensions.
    3. Merge the two image resources.

    Make sure the thumbnail keeps the original aspect ratio, so that it can be centered in the background.

    The coordinates for the thumbnail when merging should be like this:
    thumb_x = (original_width - thumb_width) / 2
    thumb_y = (original_height - thumb_height) / 2

      Thanks for reply.

      Here is the code I'm using.

      // image I want to resize
      $img = imagecreatefromjpeg('1.jpg');
      
      // canvas size
      $h_canvas = imagesx($img);
      $w_canvas = imagesy($img);
      
      $canvas = imagecreatetruecolor($h_canvas, $w_canvas);
      
      // new image size
      $h_image = $h_canvas * 0.8;
      $w_image = $w_canvas * 0.8;
      
      // start pixel
      $x_image = $h_canvas * 0.1;
      $y_image = $w_canvas * 0.1;
      
      imagecopyresampled($canvas, $img, $x_image, $y_image, 0, 0, $h_image, $w_image, $h_canvas, $w_canvas);
      imagedestroy($img);
      imagejpeg($canvas, '11.jpg', 100);
      imagedestroy($canvas);
      

      Remember, you've got 2 images in memory.

        Nice one.
        If you're done, select "Thread Resolved" from "Thread Tools" above.

          Write a Reply...