Hi
I have a script that uploads images to a server and adds the details to a database, these parts work ok, I am also trying to resizew the images to a maximum width or height whilst keeping their ratio the same, this part isn’t working
The code I am using is:
<?php
error_reporting( E_ALL );
include("config.inc.php");
include('../includes/connect_inc.php');
// initialization
$result_final = "";
$counter = 0;
// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png'
);
// GD Function List
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'GIF',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG'
);
// Fetch the photo array sent by add-photos.php
$photos_uploaded = $_FILES['photo_filename'];
// Fetch the photo caption array
$photo_caption = $_POST['photo_caption'];
while( $counter <= count($photos_uploaded) )
{
if($photos_uploaded['size'][$counter] > 0)
{
if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
{
$result_final .= "File ".($counter+1)." is not a photo<br />";
}
else
{
mysql_query( "INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" );
$new_id = mysql_insert_id();
$filetype = $photos_uploaded['type'][$counter];
$extention = $known_photo_types[$filetype];
$filename = $new_id.".".$extention;
mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );
// Store the orignal file
copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename);
//resize photos to maximum height or width
//define parameters - maximum height & width for new images
$image_max_width=599;
$image_max_height=599;
$max_dimension=800;
// Grab the width and height of the image.
list($width,$height) = getimagesize( $images_dir."/".$filename);
// If the max width input is greater than max height we base the new image off of that, otherwise we
// use the max height input.
// We get the other dimension by multiplying the quotient of the new width or height divided by
// the old width or height.
echo 'Line: ' . __LINE__ . '$images_dir = ' . $images_dir . '<br/>';
echo 'Line: ' . __LINE__ . '$width = ' . $width . '<br/>';
echo 'Line: ' . __LINE__ . '$width = ' . $height . '<br/>';
echo 'Line: ' . __LINE__ . '$image_max_width = ' . $image_max_width . '<br/>';
echo 'Line: ' . __LINE__ . '$image_max_height = ' . $image_max_height . '<br/>';
if($image_max_width > $image_max_height){
if($image_max_width > $max_dimension){
$newwidth = $max_dimension;
} else {
$newwidth = $image_max_width;
echo 'Line: ' . __LINE__ . '$newwidth = ' . $newwidth . '<br/>';
}
$newheight = ($newwidth / $width) * $height;
} else {
if($image_max_height > $max_dimension){
$newheight = $max_dimension;
} else {
$newheight = $image_max_height;
}
$newwidth = ($newheight / $height) * $width;
}
echo 'Line: ' . __LINE__ . '$newwidth = ' . $newwidth . '<br/>';
echo 'Line: ' . __LINE__ . '$newheight = ' . $newheight . '<br/>';
// Create temporary image file.
$tmp = imagecreatetruecolor($newwidth,$newheight)or die('Cannot Initialize new GD image stream');
// Copy the image to one with the new width and height.
echo 'Line: ' . __LINE__ . '$filename = ' . $filename . '<br/>';
echo 'Line: ' . __LINE__ . 'photoname = ' . $photos_uploaded['tmp_name'][$counter] . '<br/>';
echo 'Line: ' . __LINE__ . '$tmp = ' . $tmp . '<br/>';
$image = imagecreatefromjpeg($images_dir.'/'.$filename)or die();
echo 'Line: ' . __LINE__ . '$image = ' . $image . '<br/>';
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
//end of resizing
// Let's get the Thumbnail size
$size = GetImageSize( $images_dir."/".$filename );
if($size[0] > $size[1])
{
$thumbnail_width = 100;
$thumbnail_height = (int)(100 * $size[1] / $size[0]);
}
else
{
$thumbnail_width = (int)(100 * $size[0] / $size[1]);
$thumbnail_height = 100;
}
// Build Thumbnail with GD 1.x.x, you can use the other described methods too
$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = "ImageCreateFrom".$function_suffix;
$function_to_write = "Image".$function_suffix;
// Read the source file
$source_handle = $function_to_read ( $images_dir."/".$filename );
if($source_handle)
{
// Let's create an blank image for the thumbnail
$destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height );
// Now we resize it
ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );
}
// Let's save the thumbnail
$function_to_write( $destination_handle, $images_dir."/tb_".$filename );
ImageDestroy($destination_handle );
//
$result_final .= "<img src='".$images_dir. "/tb_".$filename."' /> File ".($counter+1)." Added<br />";
}
}
$counter++;
}
//header('Location: add-photos.php');
// Print Result
echo <<<__HTML_END
<html>
<head>
<title>Photos uploaded</title>
</head>
<body>
$result_final
</body>
</html>
__HTML_END;
?>
The values of the variables that I have echoed are:
Line: 65$images_dir = ../photos Line: 66$width = 1600 Line: 67$width = 1200 Line: 68$image_max_width = 599 Line: 69$image_max_height = 599 Line: 86$newwidth = 798.66666666667 Line: 87$newheight = 599 Line: 91$filename = 86.jpg Line: 92photoname = C:\wamp\tmp\php119E.tmp Line: 93$tmp = Resource id #10 Line: 95$image = Resource id #12