PHP - How do you secure a unique variable name?
- by 102319141763223461745
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.
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);
?