Main Topics
Browse All TopicsHello everyone,
I would like some help about resizing images. I am developing an e-commerce site for selling cloths. I have create a control panel in order the administrator of the website to add new products, edit products and general to have control of his stock.
When the administrator adds a new product in the database he uploads also the images for the specific product.[In the site I am using two type of images for the products, the small one and a bigger one which I am using for different pages.] I have manage to write the code for uploading the images to the server using the following code:
PHP Code:
//Check for an product image
if (is_uploaded_file($_FILES[
{
if (move_uploaded_file($_FILE
{
echo " The image for product has been uploaded! ";
}else {
echo "The image for product could not be uploaded";
$image='';
}
$image=$_FILES['image_smal
echo "image". $image;
} else {
$image='';
}
the form code
HTML Code:
<form action="" method="post" enctype="multipart/form-da
What I would like help from you is how to resize the images that the administrator uploads to the server.
I would like when the user uploads the image to resize it into two type of dimensions.
The first to be the small image with dimension width:55pixels height:66pixels and the second, width:154, height:198.
I would like also when they are resized to be saved in different folder, for the small in the folder small and for the large images in the folder image.
I tryied to use the imagecopyresampled function but I did not manage to do it.
However, I would really appreciated if you could help me find how to do it.
Thank you very much,
Xenia
Edit/Delete Message
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You may be interested in a short tutorial I wrote on this a while ago: http://www.nicholassolutio
The logic is usually somthing like this.
1. Upload the image into the "tmp" directive.
2. Create a thump from that directive and either save it to file or database blob.
3. Create a (altered size) image from the original to fit into the site.
step three is to ensure that the image size doesnt "mess up" you template if any. So we parse the image twice. 1 to create a thump and 2 to create the image we like as result for some template.
lets say the max sizes of the thump will be 150x150 and the "larger" version should be somthing like 700x700max.
This is how we do it :D
// Function to calculate the image size //
function calc_dim($old_x, $old_y, $new_x, $new_y){
if($old_x > $old_y){
$t_new_x = $new_x;
$t_new_y = $old_y * ($new_y / $old_x);
}
if($old_x < $old_y){
$t_new_x = $old_x * ($new_x / $old_y);
$t_new_y = $new_y;
}
if($old_x == $old_y){
$t_new_x = $new_x;
$t_new_y = $new_y;
}
$size = array ('width' => $t_new_x, 'height' => $t_new_y);
return($size);
}
function create_image($source, $new_x, $new_y, $type, $location, $name){
// Create a source pointer
switch($type){
case "image/jpeg" :
$pointer = ImageCreateFromJpeg($sourc
$trigger = "1";
break;
case "image/pjpeg" :
$pointer = ImageCreateFromJpeg($sourc
$trigger = "1";
break;
case "image/png" :
$pointer = ImageCreateFromPng($source
$trigger = "1";
break;
case "image/xpng" :
$pointer = ImageCreateFromPng($source
$trigger = "1";
break;
case "image/gif" :
$pointer = imagecreatefromgif($source
$trigger = "2";
break;
}else{
echo "type not allowed";
}
if($trigger = "1"){
$old_x = imageSX($pointer);
$old_y = imageSY($pointer);
// Compute size //
$size = $this->calc_dim($old_x, $old_y, $new_x, $new_y);
if($thump_canvas = ImageCreateTrueColor($size
if(ImageCopyResampled($thu
Imagejpeg($thump_canvas, $location.$name.'.jpg', 100);
Imagedestroy($thump_canvas
return(true);
}else{
return(false);
}
}else{
return(false);
}
}else{
$old_x = imageSX($pointer);
$old_y = imageSY($pointer);
$size = compute_thump_size($old_x,
if($thump_canvas =imagecreatetruecolor($siz
$clr['red']=255;
$clr['green']=255;
$clr['blue']=255;
if($pallet =imagecolorallocate($thump
imagefill($thump_canvas,0,
if(ImageCopyResampled($thu
Imagejpeg($thump_canvas, $location.$name.'.jpg', 100);
Imagedestroy($thump_canvas
return(true);
}else{
return(false);
}
}else{
return(false);
}
}else{
return(false);
}
}
}
// You are using the upload directives so we dont actually need to "move" the file anymore and worry about rights etc, We just create 2 new ones like this //
if(is_uploaded_file($_FILE
$path = './images/uploads/original
$thpath= './images/uploads/thumps/'
// Thump //
$width = "150"; $height="150";
create_image($_FILES['user
//Image
$width = "700"; $height="700";
create_image($_FILES['user
}else{
//no uploaded file
}
Good Luck!
First of all thank you all for your response, however I feel a little confused.
Can I adjust my code with a code that will resize and put in two diffeent folders the images?
Also how I can install the GD graphic?The php is not installed in my computer, as I connect with FTP with an exernal company that host
php and mysql.
dutchclan I used your code but it didn't run
So please more help?
Thank you,
Xenia
form page
<form enctype="multipart/form-da
<input type="file" name="uploadfile"/>
<input type="submit"/>
--------------------------
upload.php page
--------------------
<?php
echo $_POST['txt'];
$uploadedfile = $_FILES['uploadfile']['tmp
$src = imagecreatefromjpeg($uploa
list($width,$height)=getim
$newwidth=600;
$newheight=($height/$width
$tmp=imagecreatetruecolor(
imagecopyresampled($tmp,$s
$filename = "images/". $_FILES['uploadfile']['nam
imagejpeg($tmp,$filename,1
imagedestroy($src);
imagedestroy($tmp);
?>
this script requires a folder called images in the same location as the script and has to have write permission on it
this script will work on jpg files. it uses GD lib (ofcourse)
inform me of updates
Xenia
try to install php and mysql on your local computer for development, and after that upload your work to the production server. it is not recommended at all that you mess around with a production server!
my code works with me just fine, create a php file with phpinfo(); and in the resulted page search for GD part, after all, gd might not be installed in the first place. also, what is your PHP version ??
The version of php is PHP Version 4.3.10 and the informaiton for GD are the following:
GD Support enabled
GD Version bundled (2.0.28 compatible)
FreeType Support enabled
FreeType Linkage with freetype
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled
The inital code I wrote that uploads images in the server is working and is strange how the new code cannot work.It says that the image was uploaded but I cannot see teh image.
Thanks
The strange thing is that when I used my code which is just upload images with the following code it did the upload:
//Check for an product image
if (is_uploaded_file($_FILES[
{
if (move_uploaded_file($_FILE
{
echo " The image for product has been uploaded! ";
}else {
echo "The image for product could not be uploaded";
$image='';
}
$image=$_FILES['image_smal
//echo "image: ". $image;
} else {
$image='';
}
So I guest if the ISP has uploads displayed or I hadn't gave the correct permisions it shouldn't work neither my code,what you recommend?
Thanks
u upload to this folder
"../../images/small_images
did u adjust that folder in the code i sent u ??
ok, lets try my code with just the upload part, shall we ?
upload.php page
--------------------
<?php
echo (move_uploaded_file($_FILE
?>
First of all thank you for your time that you help me, Ok it was uploaded to the server however it was not resized to smaller.
I put in the line of code:
$filename = "../images/small_images/".
instead of :
$filename = "images/". $_FILES['uploadfile']['nam
<?php
echo $_POST['txt'];
$uploadedfile = $_FILES['uploadfile']['tmp
$src = imagecreatefromjpeg($uploa
list($width,$height)=getim
$newwidth=600;
$newheight=($height/$width
$tmp=imagecreatetruecolor(
imagecopyresampled($tmp,$s
$filename = "../images/small_images/".
imagejpeg($tmp,$filename,1
imagedestroy($src);
imagedestroy($tmp);
?>
Where do you think might be the problem
Thank you
Xenia
<?php
echo $_POST['txt'];
$uploadedfile = $_FILES['uploadfile']['tmp
$src = imagecreatefromjpeg($uploa
list($width,$height)=getim
$newwidth=50; //<------ here
$newheight=($height/$width
$tmp=imagecreatetruecolor(
imagecopyresampled($tmp,$s
$filename = "../images/small_images/".
imagejpeg($tmp,$filename,1
imagedestroy($src);
imagedestroy($tmp);
?>
Ok, I just copy and paste what you write but again it doesn't resize it.
I have the following problems:
The first one is that when I succesfully upload an image to the server in the specific folder(which has full privileges)it changes the privilege
to 666 and cannot display the page when I go to the specific url in the browser.
However,when I am going to the server and change again the privileges of the image,it displays the normal size and not the smaller image.
when I go to the absolute url.
try it on my own site
http://www.alatebaa.com/im
the very very same script, on ur very own PHP version !!!
if it is not folder permission, then it is something magic or from March :)
Yes you are right I test in your site and is working.
Ok so the directory you are putting in the following code:
$filename = "../images/small_images/".
is which?
I think also that the problem is with the permissions,however the weard is that I change the privileges but somehow it changes again.
I tryied to change the path in order to store the image general in the website outside from the images folder and is working.
I have an Ftp software called Ipswitch WS_FTP professional and I change from this program.
I had change again with the same program the permission and it was ok.Is there any alterenative way to change them?
I would like to ask you a last question.I want to store in a differnet folder the large images with specific dimensions and in differnet folder
the small images with also specific dimensions.How I can do it regarding your code?
Thank you again,
Xenia
It changes again the right , I can't understand why. I change also the previleges from Dreamweaver also but the same.
What else you think to try in order to change the permissions?
ALso regarding to the follwing code it doesn't resize it now,can you have it a look?
$uploadedfile = $_FILES['uploadfile']['tmp
copy($_FILES['uploadfile']
echo $_POST['txt'];
$uploadedfile = $_FILES['uploadfile']['tmp
//copy($_FILES['uploadfile
$src = imagecreatefromjpeg($uploa
list($width,$height)=getim
echo (move_uploaded_file($_FILE
echo "file name ". $_FILES['uploadfile']['nam
echo "filename ". $filename;
?>
<img src="../<? echo $_FILES['uploadfile']['nam
Thanks
for the permission, i connect to ftp via IE, and right click a folder -> properties and give write (or full control) to everyone. if u can't manage permisions, contact ur isp
what is that code ?? it is a bite of each post !!
use this
<?php
$uploadedfile = $_FILES['uploadfile']['tmp
copy($_FILES['uploadfile']
$src = imagecreatefromjpeg($uploa
list($width,$height)=getim
$newwidth=100;
$newheight=($height/$width
$tmp=imagecreatetruecolor(
imagecopyresampled($tmp,$s
$filename = "images/". $_FILES['uploadfile']['nam
imagejpeg($tmp,$filename,1
imagedestroy($src);
imagedestroy($tmp);
?>
<img src="<?=$filename?>">
Business Accounts
Answer for Membership
by: xenoulaPosted on 2006-05-15 at 02:46:38ID: 16680893
sorry but i put it in a wrong cateogry , I wanted to put it in php