Umair Ayub
Umair Ayub

Reputation: 21371

Resize image to certain size without cropping with PHP

Original Image:

enter image description here

Desired output: (I added black border for the sack of understanding)

enter image description here

I want to resize image to 200/200 without cropping. See the desired output.

I have this code

<?php
// function created by www.thewebhelp.com

if (!function_exists("create_square_image")) {

    function create_square_image($original_file, $destination_file = NULL, $square_size = 96) {

        if (isset($destination_file) and $destination_file != NULL) {
            if (!is_writable($destination_file)) {
                echo '<p style="color:#FF0000">Oops, the destination path is not writable. Make that file or its parent folder wirtable.</p>';
            }
        }

        // get width and height of original image
        $imagedata = getimagesize($original_file);
        $original_width = $imagedata[0];
        $original_height = $imagedata[1];

        if ($original_width > $original_height) {
            $new_height = $square_size;
            $new_width = $new_height * ($original_width / $original_height);
        }
        if ($original_height > $original_width) {
            $new_width = $square_size;
            $new_height = $new_width * ($original_height / $original_width);
        }
        if ($original_height == $original_width) {
            $new_width = $square_size;
            $new_height = $square_size;
        }

        $new_width = round($new_width);
        $new_height = round($new_height);

        // load the image
        if (substr_count(strtolower($original_file), ".jpg") or substr_count(strtolower($original_file), ".jpeg")) {
            $original_image = imagecreatefromjpeg($original_file);
        }
        if (substr_count(strtolower($original_file), ".gif")) {
            $original_image = imagecreatefromgif($original_file);
        }
        if (substr_count(strtolower($original_file), ".png")) {
            $original_image = imagecreatefrompng($original_file);
        }

        $smaller_image = imagecreatetruecolor($new_width, $new_height);
        $square_image = imagecreatetruecolor($square_size, $square_size);

        imagecopyresampled($smaller_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

        if ($new_width > $new_height) {
            $difference = $new_width - $new_height;
            $half_difference = round($difference / 2);
            imagecopyresampled($square_image, $smaller_image, 0 - $half_difference + 1, 0, 0, 0, $square_size + $difference, $square_size, $new_width, $new_height);
        }
        if ($new_height > $new_width) {
            $difference = $new_height - $new_width;
            $half_difference = round($difference / 2);
            imagecopyresampled($square_image, $smaller_image, 0, 0 - $half_difference + 1, 0, 0, $square_size, $square_size + $difference, $new_width, $new_height);
        }
        if ($new_height == $new_width) {
            imagecopyresampled($square_image, $smaller_image, 0, 0, 0, 0, $square_size, $square_size, $new_width, $new_height);
        }


        // if no destination file was given then display a png      
        if (!$destination_file) {
            imagepng($square_image, NULL, 9);
        }

        // save the smaller image FILE if destination file given
        if (substr_count(strtolower($destination_file), ".jpg")) {
            imagejpeg($square_image, $destination_file, 100);
        }
        if (substr_count(strtolower($destination_file), ".gif")) {
            imagegif($square_image, $destination_file);
        }
        if (substr_count(strtolower($destination_file), ".png")) {
            imagepng($square_image, $destination_file, 9);
        }

        imagedestroy($original_image);
        imagedestroy($smaller_image);
        imagedestroy($square_image);
    }

}

create_square_image("image.jpg", "sample_thumb.jpg", 200);
?>

<h2>Original image</h2>
<h2><img src="image.jpg" />
</h2>

<h2>The created square thumbnail</h2>
<h2><img src="sample_thumb.jpg" />
</h2>

But my code is outputting like this

enter image description here

Upvotes: 2

Views: 1557

Answers (2)

Umair Ayub
Umair Ayub

Reputation: 21371

I have found the solution that works for both situation whether image is wide or tall.

  $whereToPut = "source.jpg"; // This is 200/200 blank white image
    $size = getimagesize($fn);
    $ratio = $size[0] / $size[1]; // width/height

    $dst_y = 0;
    $dst_x = 0;

    if ($ratio > 1) {
        $width = 200;
        $height = 200 / $ratio;
        $dst_y = (200 - $height) / 2;
    } else {
        $width = 200 * $ratio;
        $height = 200;
        $dst_x = (200 - $width) / 2;
    }

    $src = imagecreatefromstring(file_get_contents($fn));
    $dst = imagecreatetruecolor($width, $height);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);

    $image1 = imagecreatefromjpeg($whereToPut);

    imagecopymerge($image1, $dst, $dst_x, $dst_y, 0, 0, imagesx($dst), imagesy($dst), 100);
    imagejpeg($image1, $finalImage); 

    // $dinalImage is your final created image.

Upvotes: 1

Linus Juhlin
Linus Juhlin

Reputation: 1273

How about using a white image that matches your dimensions? I.e you get a white image that is your desired size (200x200) and merge it with your other image.

// First image is the white image that has the dimension you want.
$image1 = imagecreatefrompng('COLOR.png'); 

// Second image is the image you want to change size of
$image2 = imagecreatefrompng('SOURCE.png'); 

// Merge the two, so that the white image is below the "Dress" 
// image you showed us in your question.
imagecopymerge($image1, $image2, 0, 0, 0, 0, 161, 200, 100);

// Output and free from memory
header('Content-Type: image/png');
echo imagepng($image1);

// Destroy images
imagedestroy($image1);
imagedestroy($image2);

Parameters for the function:

bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )

Last parameter is percentage(alpha) so if you don't want a white backround then you just set it to 0, and it will be transparent.

EDIT: Check out the docs so that you get the correct position for the second image here.

EDIT 2: The above code will produce the following image(provided the color image is black):

enter image description here

Upvotes: 3

Related Questions