Well, I think I would setup a way to get a new filename, to distinguish each of the thumbnails, then use arrays and foreach:
functions.php :
PHP Code:
<?php
function createThumbnail($filename) {
require 'path.php';
if(preg_match('/[.](jpg)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$tn = '';
foreach ($final_width_of_image as $nx){
$ny = floor($oy * ($nx / $ox));
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}
imagejpeg($nm, $path_to_thumbs_directory . $nx . 'x' . $ny . $filename,70);
$tn[] = '<img src="' . $path_to_thumbs_directory . $nx . 'x' . $ny . $filename . '"
alt="image" />';
}
$tn = implode('<br />', $tn);
$tn .= '<br />Congratulations. Your file has been successfully uploaded, and
3 thumbnails have been created.';
echo "<p align=center>". $tn . "</p>";
}
?>
path.php:
PHP Code:
<?php //path
$final_width_of_image = Array(200, 500, 800);
$path_to_image_directory = 'images/';
$path_to_thumbs_directory = 'images/thumbs/';
?>
I'd be tempted to be a bit more precise about the output file's name and (especially) extension and type, but that was inherent in the original code, and appears to work. Also rather than have to remember all the weird filenames, I'd make individual folders in the thumbnail directory by the widths, that way the filename could be preserved throughout, only the path would vary (so the 200px wide thumb would be in thumbs/200/ and so on for the other sizes. Taking that all together, I'd do functions.php like so:
PHP Code:
<?php
function createThumbnail($filename) {
require 'path.php';
$type = exif_imagetype($filename); // get image type as integer
if ($type) {
$type = image_type_to_mime_type($type); // get mime type as text, ex: image/jpeg
$type2 = explode('/', $type); // with next line, get just the text of image type
$type2 = $type2[1]; // ex: jpeg
if(!preg_match("/^(jpeg)|(gif)|(png)$/", $type2)){die('unsupported image type');}
$im = call_user_func("imagecreatefrom$type2", $filename); // use image type text to create its standard function to create image object
} else {die('cannot read filetype');}
$bname = basename($filename);
$filename = preg_replace("/\.[^\.]*$/", '', $bname);
$ext = str_replace($filename, '', $bname);
$ox = imagesx($im);
$oy = imagesy($im);
if(!file_exists($path_to_image_directory)) {
if(!mkdir($path_to_image_directory)) {
die("There was a problem. Please try again!");
}
}
if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}
foreach ($final_width_of_image as $nx){
$ny = floor($oy * ($nx / $ox));
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(!file_exists($path_to_thumbs_directory . $nx . '/')) {
if(!mkdir($path_to_thumbs_directory . $nx . '/')) {
die("There was a problem. Please try again!");
}
}
$args = Array($nm, $path_to_thumbs_directory . $nx . '/' . $filename . $ext);
if($type2 === 'jpeg'){$args[] = 70;} // image quality 0 worse 100 best
else if($type2 === 'png'){$args[] = 9;} // compression 0 least 9 most
call_user_func_array("image$type2", $args);
imagedestroy($nm);
$tn[] = '<img style="vertical-align: top;" src="' . $path_to_thumbs_directory . $nx . '/' . $filename . $ext . '" alt="image" />';
}
imagedestroy($im);
$tn = implode(' ', $tn);
$tn = 'Congratulations. Your file has been successfully uploaded, and 3 thumbnails have been created:<br />' . $tn;
echo "<p align=center>". $tn . "</p>";
}
?>
Also adds imagedestroy to save memory, makes sure the images folder is there, and can accept an image (path and filename) from anywhere on the server. One thing I'm not clear on though is - do you want all the thumbnails to be jpg regardless of their original format? My latest code (last code block) preserves the original format and extension, the code you had (and my first functions.php code) made only jpeg images but also preserved the original extension, which would result in .png and .gif extensions for jpeg format images (could be a problem). If you do want all jpeg format thumbnails, we could get those with the correct .jpg extension and pass the thumbnail filename(s) along as needed with the new .jpg extension as applicable. Something to think about.
Bookmarks