Reputation: 61
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);
?>
Upvotes: 0
Views: 186
Reputation: 70490
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
Upvotes: 1
Reputation: 4193
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.
Upvotes: 0