php resizing

Sep 9, 2020 PHP resizing

#Preprocessing for handling images

ImageCreateFromJPEG ($ filename)

Now you can handle it in memory (in this program) #Resize image

bool imagecopyresampled (resource $ dst_image, resource $ src_image,
int $ dst_x, int $ dst_y, int $ src_x, int $ src_y, int
$ dst_w, int $ dst_h, int $ src_w, int $ src_h)

Parameters dst_image Image link resource to copy to. src_image Source image link resource. dst_x The x coordinate of the destination. dst_y The y coordinate of the copy destination. src_x The x coordinate of the copy source. src_y The y coordinate of the copy source. dst_w The width of the copy destination. dst_h The height of the copy destination. src_w The width of the copy source. src_h The height of the copy source.

Return value Returns TRUE on success and FALSE on failure.

#Output or save image

bool imagejpeg (resource $ image [, string $ filename [, int $ quality]])

argument image Specify the image resource.

filename If you want to save the image resource to a file, specify the file name. quality Specify the image quality from 0 (low quality) to 100 (high quality). The default is 75. Return value Returns true if the image was successfully output / created, flase otherwise.

#Actually resizing

$ filename = "image path"
if (mime_content_type ($ filename) =='image / jpeg') {
          $ image = ImageCreateFromJPEG ($ filename);
          $ size = getimagesize ($ filename);
        } else {
          $ image = ImageCreateFromPNG ($ filename);
          $ size = getimagesize ($ filename);
        }



        // Create a blank image
        $ image2 = ImageCreateTrueColor (640, 480);
        // Resize process
        imagecopyresampled ($ image2, $ image, 0, 0, 0, 0, 640, 480, $ size [0], $ size [1]);

        // Save
        if (mime_content_type ($ filename) =='image / jpeg') {
          imagejpeg ($ image2, $ filename);
        } else {
          imagepng ($ image2, $ filename);
        }

        // Memory release
        imagedestroy ($ image);

#Resize problem The actual size is “400px in width and 180px in height”, but the width (as a result of specifying “200px” in the width property, the aspect ratio is maintained and the height becomes “90px”. If you specify either, the height or width can be determined automatically while maintaining the ratio of the original image.

width: auto;

#Resize formula Original image horizontal: X1, vertical: Y1 Image after resizing Horizontal: X2, Vertical: Y2 When When X2 is set to an arbitrary value, the value of Y2 while maintaining the aspect ratio is Y2 = X2 / X1 * Y1

#Practice

        // Supports retina display
        $ resize_width = 462 * 2; // Determine width
        $ resize_height = $ resize_width / $ size [0] * $ size [1]; // Put out the vertical without breaking the ratio
        // Create a blank image
        $ image2 = ImageCreateTrueColor ($ resize_width, $ resize_height);
        // Resize process
        imagecopyresampled ($ image2, $ image, 0, 0, 0, 0, $ resize_width, $ resize_height, $ size [0], $ size [1]);

Complete


if (mime_content_type ($ filename) =='image / jpeg') {
          $ image = ImageCreateFromJPEG ($ filename);
          $ size = getimagesize ($ filename);
        } else {
          $ image = ImageCreateFromPNG ($ filename);
          $ size = getimagesize ($ filename);
        }


        // Supports retina display
        $ resize_width = 462 * 2; // Determine width
        $ resize_height = $ resize_width / $ size [0] * $ size [1]; // Put out the vertical without breaking the ratio
        // Create a blank image
        $ image2 = ImageCreateTrueColor ($ resize_width, $ resize_height);

        $ x = 0;
        $ y = 0;
        $ x = floor (($ size [0]-$ size [1]) / 2); // Center of x-axis to cut
        $ y = floor (($ size [1]-$ size [0]) / 2); // Center of x-axis to cut
        // Resize process
        imagecopyresampled ($ image2, $ image, 0, 0, $ x, $ y, $ resize_width, $ resize_height, $ size [0], $ size [1]);

        // Save
        if (mime_content_type ($ filename) =='image / jpeg') {
          imagejpeg ($ image2, $ filename);
        } else {
          imagepng ($ image2, $ filename);
        }

        // Memory release
        imagedestroy ($ image);

Reference: https://blog.ver001.com/php_image_resize_object-fit/ https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q11117777576 https://qiita.com/mikakane/items/290301a308277a384c2d http://iekasegu.work/column.php?no=144