tags:

views:

72

answers:

3

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);
?>
A: 

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.

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

This is not an answer to your problem because I don't know what it is exactly.

But if you have a function and define parameters like $foo:

function($foo) {
    // function body
}

then $foo is only valid inside the function body.

If you call the function, every execution of the function has its own $foo variable (in a way) so they have no influence on each other.


The problem with imagejpg() might be, that it outputs the image directly as raw data if you don't specify a filename to save it to. So you might have to change this, depending on your context.

Felix Kling
+1  A: 

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

Wrikken
"[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...
Wrikken