Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 697
  • Last Modified:

creating thumbnails, imagecopyresampled() in php

I am trying to create thumbnails in php. I have attached the code which works great except the files with bigger dimensions (width > 1600 aprox., Height > 1600 aprox.)
When I try to upload a wider image, it creates a blank thumbnail.
Any ideas/suggestions?
if(isset($_POST['submit']))
   {
     include_once('conn.php');
   	 $id = $_POST['id'];
	  
     $new_height=100;
     $new_width=150;
     $allowed_types = array( 
     'image/pjpeg', 
     'image/gif', 
     'image/png', 
     'image/jpeg'); 
 
     if(in_array($_FILES['thefile']['type'], $allowed_types)) 
     {
 
       copy ($_FILES['thefile']['tmp_name'], $_FILES['thefile']['name']) or die     ("Could not copy");
       $imagefile=$_FILES['thefile']['name'];
       list($width, $height) = getimagesize($_FILES['thefile']['name']);
		 
	   if($height){
	   $percentage = round(($new_width * 100/$width));
	   $new_height = ($height*$percentage/100);
	   }
	   
       $image_p = imagecreatetruecolor($new_width,$new_height);
       if ($_FILES['thefile']['type'] == "image/gif") 
       {
           $img = @imagecreatefromgif($imagefile);
           imagecopyresampled($image_p, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
           $thename=$_FILES['thefile']['name'];
           $thenames="thumb$thename";
           $location="thumbs/$thenames";
           imagegif($image_p,$location, 100);
 
       }
       else
       {
         $img = @imagecreatefromjpeg($imagefile);
         imagecopyresampled($image_p, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
         $thename=$_FILES['thefile']['name'];
         $thenames="thumb$thename";
         $location="$thenames";
         imagejpeg($image_p,$location, 100);
       }
	    $sql = "UPDATE `tbl_people` SET image ='$thename', thumbnail ='$thenames' WHERE people_id = $id ";
		  
		  $rs = mysql_query($sql,$cn);
	      header("Location:../people_list.php");
     }
	 
	 
     else
     {
       print "<br>Please check your file size and file format (Only Gifs and Jpegs are supported). <br /> Back to <A href='../people_update.php?id=$id'>Upload photo</a>";
     }
   }
 
 mysql_close($cn);

Open in new window

0
martyje
Asked:
martyje
  • 3
1 Solution
 
FreakTrapCommented:
Try editing the two instances of the attached lines to remove the @.


This should give you an error now when uploading the images.  Something along the lines of "Insufficient memory available for allocation."  AFAIK, this is an issue with the restrictions placed upon the PHP executable by your hosting provider; however, if this is not the error you receive or if you are hosting the server yourself, please post the new error output after making the above changes.
<?php
     $img = @imagecreatefromjpeg($imagefile);
?>
becomes:
<?php
     $img = imagecreatefromjpeg($imagefile);
?>

Open in new window

0
 
martyjeAuthor Commented:
Didn't do anything. same results. I think the problem is in imagecopyresampled() function. Not exactly sure what is it but this is where it's acting ODD.
0
 
martyjeAuthor Commented:
Ok, yea got the error running the script:
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 1616 bytes) in /test/upload.php on line 107
0
 
Roger BaklundCommented:
To allow PHP to use as much memory as it needs, use the below code at the start of your script:
ini_set('memory_limit',-1);

Open in new window

0
 
martyjeAuthor Commented:
Thanks much, sorry for the late grading :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now