/    Sign up×
Community /Pin to ProfileBookmark

how to create multiple thumbnails from one image

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

200×200
500×500
800×800

how to generate them ??

[code]
<?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>
[/code]

this is functions.php

[code]
<?
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>”;
}
?>
[/code]

this is path.php

[code]
<?
$final_width_of_image = 200;
$path_to_image_directory = ‘images/’;
$path_to_thumbs_directory = ‘images/thumbs/’;
?>
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 15.2016 — [code=php]
for($size = 200; $size <= 800; $size += 300) {
// your resizing code here, using $size wherever you'd specify the dimensions
// (and probably also in the file name?)
}
[/code]
×

Success!

Help @vinpkl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...