Solved

Gallery problem with large size photos

Posted on 2007-03-24
4
675 Views
Last Modified: 2013-12-04
In my photo gallery that I created, I use an upload form.  Everthing's working fine so far.  I upload a photo, an automatiic thumbnail is generated, placed in my gallery and when you click on it, the larger size opens.  ALL FINE.  BUT, when I try to upload a photo that's larger in size, say that outputted from a digital camera (2.11 MB and 2592pixels x 1728pixels) the photo IS STILL UPLOADED, but the thumbnail will not appear (only an "x" as a place holder) and when you click on it the full size image is HUGE. My php.ini file must be correct in that it's allowing it to upload, but I think something's incorrect in my script that won't resize the thumbnail and full size image correctly for these larger size photos.  ACTUALLY, I just checked my "photos" folder in my directory (where I'm uploading the photos to) and the thumbnai is NOT being generated for this size of an image.  Can you look through my code and see if you can tell where the problem might be?  This is my "upload.php" file.

ALSO, I read somewhere that I migt have to add a "ini_set()" somewhere in my php.ini file, so when the photo uploads it read this script just for that moment to allow the file size to show up correctly?  Is that wrong?

<?php

// initialization
$result_final = "";
$counter = 0;

// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/bmp' => 'bmp',
'image/x-png' => 'png');
       
// GD Function Suffix List
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG');
                              
// Fetch the photo array sent by preupload.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);                                          
// Let's get the original image size
                               
$size = GetImageSize( $images_dir."/".$filename );

// First Create Thumbnail!
// Thumbnail Settings
        $Config_tbwidth_wide = 150; // width of wide image
        $Config_tbheight_wide = 125; // height of wide image

        $Config_tbwidth_tall = 125; // width of tall image
        $Config_tbheight_tall = 150; // height of tall image

// The Code
        if($size[0] > $size[1]){
            $thumbnail_width = $Config_tbwidth_wide;
            $thumbnail_height = (int)($Config_tbwidth_wide * $size[1] / $size[0]);

            if($thumbnail_height > $Config_tbheight_wide){
                $thumbnail_height = $Config_tbheight_wide;
                $thumbnail_width = (int)($Config_tbheight_wide * $size[0] / $size[1]);
            }
        }else{
            $thumbnail_width = (int)($Config_tbheight_tall * $size[0] / $size[1]);
            $thumbnail_height = $Config_tbheight_tall;

            if($thumbnail_width > $Config_tbwidth_tall){
                $thumbnail_width = $Config_tbwidth_tall;
                $thumbnail_height = (int)($Config_tbwidth_tall * $size[1] / $size[0]);
            }
        }

// Build Thumbnail with GD 2.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 a blank image for the thumbnail
$destination_handle = ImageCreateTrueColor ( $thumbnail_width, $thumbnail_height );

// Now we resize it
ImageCopyResampled( $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, 100 );
ImageDestroy($destination_handle );

// Lets resize the image if its width is greater than 500 pixels
// Resized image settings
if ($size[0] > '500'){
        $Config_width_wide = 500; // width of wide image
        $Config_height_wide = 475; // height of wide image

        $Config_width_tall = 375; // width of tall image
        $Config_height_tall = 400; // height of tall image

// The Code
        if($size[0] > $size[1]){
            $image_width = $Config_width_wide;
            $image_height = (int)($Config_width_wide * $size[1] / $size[0]);

            if($image_height > $Config_height_wide){
                $image_height = $Config_height_wide;
                $image_width = (int)($Config_height_wide * $size[0] / $size[1]);
            }
        }else{
            $image_width = (int)($Config_height_tall * $size[0] / $size[1]);
            $image_height = $Config_height_tall;

            if($image_width > $Config_width_tall){
                $image_width = $Config_width_tall;
                $image_height = (int)($Config_width_tall * $size[1] / $size[0]);
            }
        }

// Build image with GD 2.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 a blank image for the image
$destination_handle = ImageCreateTrueColor ( $image_width, $image_height );

// Now we resize it
ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $image_width, $image_height, $size[0], $size[1] );
}

// Let's save the image
$function_to_write( $destination_handle, $images_dir."/".$filename, 90 );
ImageDestroy($destination_handle );

}

$result_final .= "<img src='".$images_dir. "/tb_".$filename."' /><br />File ".($counter+1)." Added Successfully!<br /><br />";

}
}
$counter++;
}
?>




 
0
Comment
Question by:e4art
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
sebastienbo earned 500 total points
ID: 18785315
check the outcommings of your width and heights for large images.
echo this image height and width :
// Let's create a blank image for the image
$destination_handle = ImageCreateTrueColor ( $image_width, $image_height );

They should be bigger  then 1 (not zero or negative) or the function will abort and do nothing.

so add the following line
$destination_handle = ImageCreateTrueColor ( $image_width, $image_height );
die("width x height : ". $image_width." x ". $image_height);

this die will echo the width and height and stop the script so that you can check the results, if the numbers are incorrect check you code again
0
 

Author Comment

by:e4art
ID: 18787306
I'm sorry, but I added this line just as you said and it didn't change any results.  I added the following line:

die("width x height : ". $image_width." x ". $image_height);

at the section below:
// Let's create a blank image for the image
$destination_handle = ImageCreateTrueColor ( $image_width, $image_height );

Was I to change the resizing code below it too?  Still having trouble and I would appreciate your advice.
Thanks.
0
 

Author Comment

by:e4art
ID: 18791928
Still not working.  Any solutions?
0
 

Author Comment

by:e4art
ID: 18812362
Anybody available for help with this file size problem?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php call to a non-object 3 32
Change text to radio button and calendar form 2 32
html input type 3 21
Divi Them Help with Full Width header 20 17
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now