I have a function to handle image uploads. Originally, it contained a goto
:
restartA:
$name = $this->random_name() . $ext;
if(file_exists($this->img_path . $name)){
unset($name);
goto restartA;
}
It seems pretty clear to me what the code does, and I think anyone who reads it would immediately understand what's happening. On the other hand, goto
is considered evil, so I rewrote it like this:
while(true){
$name = $this->random_name() . $ext;
if(file_exists($this->img_path . $name)){
unset($name);
}else{
break; //weve got a unique name
}
}
Here's the context in which the code excerpt above appears. In place of some code which I am not allowed to post, I have substituted an array of 3 static sizes.
public function original_img_upload()
{
$img_name = 'original_image';
$image_name = $_FILES[$img_name]['name'];
$image_temp = $_FILES[$img_name]['tmp_name'];
$image_type = $_FILES[$img_name]['type'];
$image_info = getimagesize($image_temp);
$valid = ['.jpg', '.jpeg', '.gif', '.png'];
$exif = [IMAGETYPE_GIF,IMAGETYPE_JPEG,IMAGETYPE_PNG];
$ext = strtolower(strrchr($image_name, "."));
$names_to_return = []; //image names to return to main scope
$sizes = [
[1000,608],
[657,400],
[329,200]
]; //image target sizes
if( !is_uploaded_file($_FILES[$img_name]['tmp_name']) ){
$this->e('bad file input');
}
if( !in_array(exif_imagetype($image_temp),$exif) && in_array($ext,$valid) ){
$this->e('file type not allowed');
}
if( !$image_info ){
$this->e('invalid image size.');
}
switch($image_type){
case 'image/png':
$image_res = imagecreatefrompng($image_temp);
break;
case 'image/gif':
$image_res = imagecreatefromgif($image_temp);
break;
case 'image/jpeg':
case 'image/pjpeg':
$image_res = imagecreatefromjpeg($image_temp);
break;
default:$this->e('bad file input');
}
for($i=0;$i <3; $i++){
$canvas = imagecreatetruecolor($sizes[$i][0],$sizes[$i][1]);//width + height
//( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresampled(
$canvas,
$image_res,
0,0,0,0,
$sizes[$i][0],
$sizes[$i][1],
$image_info[0],
$image_info[1]
);
//create random destination file name
while(true){
$name = $this->random_name() . $ext;
if(file_exists($this->img_path . $name)){
unset($name);
}else{
break; //weve got a unique name
}
}
$destination = $this->img_path . $name;
//write image
switch(strtolower($image_type))
{
case 'image/png':
imagepng($canvas,$destination);
break;
case 'image/gif':
imagegif($canvas,$destination);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($canvas,$destination,100);
break;
}
//push image name to return images
array_push($names_to_return,$this->args['base_url'].'img/'. $name);
//unset resources and free memory
unset($canvas,$name,$destination);
}
return $names_to_return;
}