Jump to content

Images not resizing on upload


bravo14

Recommended Posts

Hi

 

I am trying to resize images as they are uploaded.  The problem I have is that the image is uploading and adding to the database, but is not resizing.

 

The code I am using is below

 

<?php
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)."'" );
                //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($_FILES['photo_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.
                if($image_max_width > $image_max_height){
                if($image_max_width > $max_dimension){
                $newwidth = $max_dimension;
                } else {
                $newwidth = $image_max_width;
                }
                $newheight = ($newwidth / $width) * $height;
                } else {
                if($image_max_height > $max_dimension){
                $newheight = $max_dimension;
                } else {
                $newheight = $image_max_height;
                }
                $newwidth = ($newheight / $height) * $width;
                }
                // Create temporary image file.
                $tmp = imagecreatetruecolor($newwidth,$newheight);
                // Copy the image to one with the new width and height.
                imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
                //end of resizing
			// Store the orignal file
			copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename);

			// 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;
?>

 

Any help would be appreciated.

Link to post
Share on other sites

You really need to re-think your logic. You are adding the image to the database before you even save it to the server. In fact. You do an insert and then follow it up with an update on that record of information you could have already had when you did the insert. Plus, you are doing things that have no purpose

				$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)."'" );

 

Why are you using addslashes() on the variable $new_id? It is an integer returned from the database. In fact, why are you using addslashes() as all?! You should be using mysql_real_escape_string().

 

The more I read of that code the more I see wrong.

 

	while( $counter <= count($photos_uploaded) )
{

You have an array. Why are you using a while() loop with an artificial counter instead of a foreach() loop/

 

list($width,$height) = getimagesize($_FILES['photo_filename']);

Why are you referencing $_FILES['photo_filename'] for getimagesize() since that value is an array? You are in a loop

 

list($width,$height) = getimagesize($_FILES['photo_filename']);

Why are you referencing the $_FILES array? You shoudl be referencing the current image in $photos_uploaded

 

				$function_to_read = "ImageCreateFrom".$function_suffix;
			$function_to_write = "Image".$function_suffix;

			// Read the source file
			$source_handle = $function_to_read ( $images_dir."/".$filename ); 

 

You cannot create a variable that has the "name" of a function and then use that variable as a function. You create a STRING, so what thePHP parser sees is something like

$source_handle = "imagecreatefromgif" ( $images_dir."/".$filename );

Complete nonsense.

 

 

I'm sorry if you feel my response is abrasive, but you need to be more aware of what you are doing and understand why you are doing it. Do, one thing at a time and test it. Then do the next thing.

Link to post
Share on other sites

Hi

 

I have added the

error_reporting( E_ALL );

 

And I am still not getting any errors displayed, it is as if the code is being ignored

Proper debugging is in place. If there are no syntax errors but it still doesn't do what you want, then there's got to be something wrong with the logic in the script. Usually logical errors in a script is easiest found by printing values that has an impact on your script's result.

 

With other words, try to echo values that is being used before if and while.

For example this line:

if($image_max_width > $image_max_height){

Replace it with this:

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){

Link to post
Share on other sites

Psycho:  I appreciate your feedback, if I am honest the page is a mashup of two tutorials, and I have tried to put it together as one.

 

I have attached the two original pages, the upload.php allows multiple uploads, and add the images to the database, and the index.php allows for one image upload but also resizes them.

 

MMDE: I will add the echos and see what I get come out.

18101_.php

18102_.php

Link to post
Share on other sites

I now have a series of errors showing, but I have a feeling that they are relate to the first one, the first error is

 

Warning: getimagesize(60.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\wamp\www\haldon\admin\upload.php on line 57

 

The entire page code 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)."'" );
                //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($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__ . '$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;
                }
                $newheight = ($newwidth / $width) * $height;
                } else {
                if($image_max_height > $max_dimension){
                $newheight = $max_dimension;
                } else {
                $newheight = $image_max_height;
                }
                $newwidth = ($newheight / $height) * $width;
                }
                // Create temporary image file.
                $tmp = imagecreatetruecolor($newwidth,$newheight);
                // Copy the image to one with the new width and height.
                imagecopyresampled($tmp,$filename,0,0,0,0,$newwidth,$newheight,$width,$height);
                //end of resizing
			// Store the orignal file
			copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename);

			// 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;
?>

 

 

Link to post
Share on other sites

I am making progress :-)

 

I just have one error now and it is

 

Warning: imagecopyresampled() expects parameter 2 to be resource, string given in C:\wamp\www\haldon\admin\upload.php on line 91

 

The offending line now says:

imagecopyresampled($tmp,$filename,0,0,0,0,$newwidth,$newheight,$width,$height);

Link to post
Share on other sites

You're doing exactly what it says you're doing: passing in a string instead of a resource. You should probably do something like:

 

$theimage = imagecreatefromjpeg($filename);
imagecopyresampled($tmp,$theimage,0,0,0,0,$newwidth,$newheight,$width,$height);

 

Not tested.

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.