Imagecrop without saving the image

I have a bunch of product preview images, but they do not match the sizes.

So, I wonder if it is possible to crop the image on the go without saving it?

These two links should show what I mean:

http://xn--nstvedhandel-6cb.dk/alpha_1/?side=vis_annonce&id=12

http://xn--nstvedhandel-6cb.dk/alpha_1/?side=vis_annonce&id=13

+5
source share
3 answers

Yes, it is possible, as I do it:

//Your Image
$imgSrc = "image.jpg";
list($width, $height) = getimagesize($imgSrc);
$myImage = imagecreatefromjpeg($imgSrc);

// calculating the part of the image thumbnail
if ($width > $height)
{
    $y = 0;
    $x = ($width - $height) / 2;
    $smallestSide = $height;
 } 
 else
 {
    $x = 0;
    $y = ($height - $width) / 2;
    $smallestSide = $width;
 }

 // copying the part into thumbnail
 $thumbSize = 100;
 $thumb = imagecreatetruecolor($thumbSize, $thumbSize);
 imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);

 //final output
 header('Content-type: image/jpeg');
 imagejpeg($thumb);

This is not a verry light operation, like the others, I also recommend that you save the sketch after creating it in your file system.

You might want to check out the PHP GD library .

+5
source

, , , , , , - .

, , (, , ) . .

, " ", , - .

, ( , ) ?

Finally, given that this is a store, they will not manually crop them to give your products the best images - and therefore the best chance of selling them?

+1
source

All Articles