thumbnail images
Posted: Wed Sep 20, 2006 7:30 pm
Hi,
I've managed to make an html form that calls a php script in order to upload an image file to a specific location on a server.
Now what I want to be able to do is create a thumbnail of the larger image when uploading to the server.
This is my code to create the thumbnail:
When this scipt is executed the thumbnail is not created.
Any ideas?
Thanks
I've managed to make an html form that calls a php script in order to upload an image file to a specific location on a server.
Now what I want to be able to do is create a thumbnail of the larger image when uploading to the server.
This is my code to create the thumbnail:
Code: Select all
<?php
//CONNECT TO SERVER AND DATABASE
$link=mysql_connect("localhost", "123", "123")
or die("Could not connect: " . mysql_error());
mysql_select_db("shop", $link)
or die(mysql_error());
//MAKE VARIABLES AVAILABLE
$prod_code = $_POST['p_code'];
$prod_name= $_POST['p_name'];
$prod_description = $_POST['p_desc'];
$image_tempname = $_FILES['p_image'] ['name'];
$prod_type = $_POST['p_type'];
$prod_unit_cost = $_POST['p_unit_cost'];
$prod_unit_price = $_POST['p_unit_price'];
$prod_stock_qty = $_POST['p_stock_qty'];
$ImageDir=$_SERVER['DOCUMENT_ROOT'] . "/shop/product_images/";
$ImageThumb=$ImageDir . "thumbs/";
$ImageName=$ImageDir . $image_tempname;
if (move_uploaded_file($_FILES['p_image']['tmp_name'], $ImageName)){
list($width, $height, $type, $attr) = getimagesize($ImageName);
if ($type>3){
echo "Sorry, but the file you uploaded was not a gif, jpg or png file.<br>";
echo "Please hit your browser's 'back' button and try again.";
} else {
//image is acceptable
//insert into product table
$insert="INSERT INTO product
(product_id, prod_code, prod_name, prod_description, prod_image, prod_type, prod_unit_cost, prod_unit_price, prod_stock_qty)
VALUES
('$prod_code',
'$prod_code',
'$prod_name',
'$prod_description',
'$image_tempname',
'$prod_type',
'$prod_unit_cost',
'$prod_unit_price',
'$prod_stock_qty')";
$insertresults=mysql_query($insert)
or die(mysql_error());
$lastpicid=mysql_insert_id();
$newfilename=$ImageDir . $prod_code . ".jpg";
$newthumbname = $ImageThumb . $prod_code . ".jpg";
if($type==2){
rename($ImageName, $newfilename);
} else {
if($type==1){
$image_old=imagecreatefromgif($ImageName);
} elseif ($type==3) {
$image_old=imagecreatefrompng($ImageName);
//convert the image to jpg
$image_jpg = imagecreatetruecolor($width, $height);
imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($image_jpg, $newfilename);
imagedestroy($image_old);
imagedestroy($image_jpg);
}
//$newthumbname = $ImageThumb . $prod_code . ".jpg";
//get the dimensions for the thumbnail
$thumb_width = $width * 0.10;
$thumb_height = $height * 0.10;
//create the thumbnail
$largeimage = imagecreatefromjpeg($newfilename);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $largeimage, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
imagejpeg($thumb, $newthumbname);
imagedestroy($largeimage);
imagedestroy($thumb);
}
}
}
?>Any ideas?
Thanks