I am using an upload script that works great:
// Create image from file
switch(strtolower($_FILES['fileField']['type'])) {
case 'image/jpeg':
$image = imagecreatefromjpeg($_FILES['fileField']['tmp_name']);
break;
case 'image/png':
$image = imagecreatefrompng($_FILES['fileField']['tmp_name']);
break;
case 'image/gif':
$image = imagecreatefromgif($_FILES['fileField']['tmp_name']);
break;
default:
exit('Unsupported type: '.$_FILES['fileField']['type']);
}
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Target dimensions for large version
$max_width = '600';
if($max_width > $old_width) {
$max_width = $old_width;
}
$max_height = ($old_height/$old_width)* $max_width;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
//Output the image to a file
imagejpeg($new, $folder.$newFileName,100);
// Destroy resources
imagedestroy($image);
imagedestroy($new);
I want to place this script inside a function so I can run it several times, each time using a different $max_width value and thereby create multiple copies of the same image in different sizes without having to duplicate all that code over and over again.
Here is my attempt:
// Create image from file
switch(strtolower($_FILES['fileField']['type'])) {
case 'image/jpeg':
$image = imagecreatefromjpeg($_FILES['fileField']['tmp_name']);
break;
case 'image/png':
$image = imagecreatefrompng($_FILES['fileField']['tmp_name']);
break;
case 'image/gif':
$image = imagecreatefromgif($_FILES['fileField']['tmp_name']);
break;
default:
exit('Unsupported type: '.$_FILES['fileField']['type']);
}
function resizeAndPlaceFile($target_max_width) {
global $image;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Target dimensions for large version
$max_width = $target_max_width;
if($max_width > $old_width) {
$max_width = $old_width;
}
$max_height = ($old_height/$old_width)* $max_width;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
//Output the image to a file
imagejpeg($new, $folder.$newFileName,100);
// Destroy resources
imagedestroy($image);
imagedestroy($new);
}
resizeAndPlaceFile('600');
As you can see, all I have done was put the code inside a function, add the global $image variable, change the hard-coded $max_width value to be equals to $target_max_width which is defined at the bottom when I call the function with the necessary parameter.
Instead of seeing a successful upload message I am seeing these symbols all over my screen:
Why does my upload script not work when placed inside a function using PHP?