0

I have a watermarking script, some of you might have seen before from one of my previous questions:

<?php 

$imagesource = $_GET['path'];

$filetype = substr($imagesource,strlen($imagesource)-4,4);

$filetype = strtolower($filetype);

if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);

if (!$image) die();

$watermark = @imagecreatefrompng('watermark_'.(imagesx($image) <= 1100 ? "port" : "lans").'.png');

$imagewidth = imagesx($image);
$imageheight = imagesy($image);

$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);

$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);

imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth,
$watermarkheight);

imagejpeg($image);

imagedestroy($image);

imagedestroy($watermark);

?> 

The script works fine, but when my client uploads a picture which is over 2000px X 2000px (or smething around there) the script outputs nothing not even an error. But, when I reduce it to 1100px X 800px for example this script outputs and displays the image. Is there any reason for this?

Is there anyway to either diagnose what is causing this and hence find a solution or to automatically re-size the image on upload?

This is the upload script I currently have:

<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      echo "<h3>".$_FILES["file"]["name"] . " already exists. </h3>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "<h3>Invalid file</h3>";
  }
?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
David Passmore
  • 6,089
  • 4
  • 46
  • 70

2 Answers2

1

Is there anyway to either diagnose what is causing this and hence find a solution

As nickb suggested, check your server's error_log. Depending on how you have PHP configured this may be in Apache's error_log, it may be in a separate file, or it may not exist and need to be turned on.

For more information, see the PHP manual's section on error handling, specifically the php.ini error_log directive.

I suspect that your script may be running out of memory. What is your memory limit set to? (<?php phpinfo(); can tell you this). Have you tried increasing it in php.ini?

or [is there any way] to automatically re-size the image on upload?

Yes. There's lots of ways to upload an image and resize it

Have you tried a search? :-)

Community
  • 1
  • 1
Josh
  • 10,961
  • 11
  • 65
  • 108
  • I have tried uping the memory limit and multiple different methods you gave me and none of them seem to resize the image. btw i have used both imagecreatetruecolour() and imagecreate neither work any ideas? – David Passmore Nov 13 '11 at 23:39
1

Thanks to Josh I have found this direct solution which I have integrated. Thanks again!

<?php
if($_REQUEST['post']==1){
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";



    if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      echo "<h3>".$_FILES["file"]["name"] . " already exists. </h3>";
      }
    else
      {

// Temporary upload image name 
$original_image = $_FILES['file']['tmp_name']; 

// Get the image dimensions 
$size=getimagesize( $original_image ); 

// Name to save the image as - in this case the same as the original 
$new_image = $_FILES['file']['name']; 

// Maximum image width 
$max_width = "1600"; 

// Maximum image height 
$max_height = "1600"; 

// Resize the image and save 
exec("convert -size {$size[0]}x{$size[1]} $original_image -thumbnail $max_widthx$max_height $new_image"); 

echo "File uploaded<br><br>"; 

echo "<img src=\"'uploads/".$new_image."\" width=\"900\"><br><br>"; 


$copy = copy($new_image, "uploads/".$new_image);
$delete = unlink($new_image);

if($copy){
      echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];

      $date = date("d/m/y");


      $query  = mysql_query ('INSERT INTO  `ap_photos_list` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'")');

      $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "6.00" , \'6" x 4"\')');
      $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "8.00" , \'5" x 7"\')');
      $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "10.00" , \'8" x 6"\')');
      $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "12.00" , \'10" x 8"\')');
      $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "15.00" , \'12" x 8"\')');
}else{
    echo "Unable to copy file";
}
      }
    }
  }
else
  {
  echo "<h3>Invalid file</h3>";
  }
?>

<br />
<br />
<h3>
<?php
if($query){
    echo "Successful";
}else{
    echo "Unsuccessful";
}
}
?>  
David Passmore
  • 6,089
  • 4
  • 46
  • 70