0

This function cropit, which I shamelessly stole off the internet, crops a 90x60 area from an existing image.

In this code, when I use the function for more than one item (image) the one will display on top of the other (they come to occupy the same output space).

I think this is because the function has the same (static) name ($dest) for the destination of the image when it's created (imagecopy).

I tried, as you can see to include a second argument to the cropit function which would serve as the "name" of the $dest variable, but it didn't work.

In the interest of full disclosure I have 22 hours of PHP experience (incidentally the same number of hours since the last I slept) and I am not that smart to begin with.

Even if there's something else at work here entirely, seems to me that generally it must be useful to have a way to secure that a variable is always given a unique name.

<?php

function cropit($srcimg, $dest) {
$im = imagecreatefromjpeg($srcimg);
$img_width = imagesx($im);
$img_height = imagesy($im);

$width = 90;
$height = 60;
$tlx = floor($img_width / 2) - floor ($width / 2);
$tly = floor($img_height / 2) - floor ($height / 2);

if ($tlx < 0)
{
$tlx = 0;
}
if ($tly < 0)
{
$tly = 0;
}
if (($img_width - $tlx) < $width)
{
$width = $img_width - $tlx;
}
if (($img_height - $tly) < $height)
{
$height = $img_height - $tly;
}
$dest =  imagecreatetruecolor ($width, $height);
imagecopy($dest, $im, 0, 0, $tlx, $tly, $width, $height);
imagejpeg($dest);
imagedestroy($dest);
}

$img = "imagefolder\imageone.jpg";
$img2 = "imagefolder\imagetwo.jpg";

cropit($img, $i1);
cropit($img2, $i2);
?>
2
  • 2
    Hahahahaha, hilarious description :) but what exactly is your question? Which name do you need made unique? Commented Jun 5, 2010 at 16:46
  • 1
    Mmh it should work to crop more images then only one with the code. What exactly do you mean with one will display on top of the other Two different images are contained in one? Btw. your second parameter does nothing, it is of no use (you don't even define a value for $i. Commented Jun 5, 2010 at 16:51

2 Answers 2

1

In this code, when I use the function for more than one item (image) the one will display on top of the other (they come to occupy the same output space).

You are creating the raw image data: you cannot serve multiple images at once in an HTTP request (you could save an unlimited amount to file ofcourse, imagejpg can take more parameters), no decent browser would know what to make of it.

If you want to overlay one image on another, look at imagecopyresampled()

I think this is because the function has the same (static) name ($dest) for the destination of the image when it's created (imagecopy).

This is not the case, as soon as your function exits $dest doesn't exist anymore (it only existed within the function scope. See http://php.net/manual/en/language.variables.scope.php

Sign up to request clarification or add additional context in comments.

2 Comments

"[Y]ou cannot serve images at once in an HTTP request", thanks that made me understand the nature of the problem. That was very helpful. Thank you. I will save to files, and then merge.
No problem. Now if someone would be so kind as to tell me why I was voted down I can amend my evil ways and prevent such drastic repercussions in future...
0

I hope I understood you. You want to save the cropped image to a filename you have in the variables $i1 and $i2?

Then the last part is probably wrong. It should be like this:

<?php
function cropit($srcimg, $filename) {
 $im = imagecreatefromjpeg($srcimg);
 $img_width = imagesx($im);
 $img_height = imagesy($im);

 $width = 90;
 $height = 60;
 $tlx = floor($img_width / 2) - floor ($width / 2);
 $tly = floor($img_height / 2) - floor ($height / 2);

 if ($tlx < 0)
 {
 $tlx = 0;
 }
 if ($tly < 0)
 {
 $tly = 0;
 }
 if (($img_width - $tlx) < $width)
 {
 $width = $img_width - $tlx;
 }
 if (($img_height - $tly) < $height)
 {
 $height = $img_height - $tly;
 }
 $dest = imagecreatetruecolor ($width, $height);
 imagecopy($dest, $im, 0, 0, $tlx, $tly, $width, $height);
 imagejpeg($dest, $filename); // Second parameter
 imagedestroy($dest);
}

imagejpeg has a second parameter which takes the filename it should be saved as.

1 Comment

Thanks imagejpeg with the second parameter should take care of it. Wow, awesome response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.