PDA

View Full Version : how to create multiple thumbnails from one image



vinpkl
09-15-2016, 03:45 AM
hi all

this is my script which creates one thumbnail from uploaded image

But now instead of one thumbnail

i want to generate 3 different size thumbnails

200x200
500x500
800x800

how to generate them ??



<?php chmod('images/', 0777);
require_once("functions.php");
require_once("path.php");

if(isset($_FILES['prod_image']))
{
if(preg_match('/[.](jpg)|(pjpeg)|(gif)|(png)$/',

$_FILES['prod_image']['name']))
{
$path=$_FILES['prod_image']['name'];
$filename = $_FILES['prod_image']['name'];
$source = $_FILES['prod_image']['tmp_name'];
$target = $path_to_image_directory . $filename;
move_uploaded_file($source, $target);
createThumbnail($filename);
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data" name="form1"

id="form1">
<input name="prod_image" type="file" id="prod_image" size="60" /> <br /><br

/>
<input type="submit" value="upload" name="submit" />
</form>
</body>
</html>


this is functions.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);

$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $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 . $filename,70);

$tn = '<img src="' . $path_to_thumbs_directory . $filename . '"

alt="image" />';
$tn .= '<br />Congratulations. Your file has been successfully uploaded, and

a thumbnail has been created.';
echo "<p align=center>". $tn . "</p>";
}
?>


this is path.php


<?
$final_width_of_image = 200;
$path_to_image_directory = 'images/';
$path_to_thumbs_directory = 'images/thumbs/';
?>

gratemacabre424
10-04-2016, 09:09 AM
Working on my side.

jscheuer1
10-04-2016, 03:31 PM
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
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 //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
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.

vinpkl
10-08-2016, 02:37 PM
thanks john for the detailed solution

vineet