PHP - How do you secure a unique variable name?
<p>This function cropit, which I shamelessly stole off the internet, crops a 90x60 area from an existing image.</p>

<p>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). </p>

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

<p>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. </p>

<p>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.</p>

<p>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. </p>

<pre><code>&lt;?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 &lt; 0)
{
$tlx = 0;
}
if ($tly &lt; 0)
{
$tly = 0;
}
if (($img_width - $tlx) &lt; $width)
{
$width = $img_width - $tlx;
}
if (($img_height - $tly) &lt; $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);
?&gt;
</code></pre>
