Jump to content

[SOLVED] Help with this code! Thought it would work but for some reason it is not workin


djpic

Recommended Posts

<?  $ImageFolderPath = "rawphotos/";

  $Folder = opendir($ImageFolderPath);
  
  $LoopCount = 0;
  
  while (false !== ($File = readdir($Folder))) {
    $FullImagePath = "$ImageFolderPath$File";
  
    echo "$FullImagePath\n";

$image = imagecreatefromjpeg($FullImagePath);

    $width = imagesx($image);
    $height = imagesy($image);
			  
    $new_width = 300;
    $new_height = 300;
			  
    $image_resized = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
			  			  
    $Destination = "test/$LoopCount.jpg";
			  
    imagejpeg($image_resized, $Destination, 100);

imagedestroy($image_resized);


$LoopCount++;
  }
  closedir($Folder);?>

 

This script only goes through the first two files in the directory and then stops.  Only rights one file to the test directory (it perfect size and such) but only echos the filename of the second one and never continues the loop?

Ok, Now I have rewritten the code.  It processes more of them now, the first 15 then stops.  I made a function instead of placing it into the loop itself.  Is there any settings that need to be changed in the GD?

<?php
  function CreateImage($ImagePath, $Count) {
  	$image = imagecreatefromjpeg($ImagePath);
    
$width = imagesx($image);
    $height = imagesy($image);
			  
    $new_width = 300;
    $new_height = 300;
			  
    $image_resized = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
			  			  
    $Destination = "test/$Count.jpg";
			  
    imagejpeg($image_resized, $Destination, 100);
imagedestroy($image_resized);
  }

  $ImageFolderPath = "rawphotos/";

  $Folder = opendir($ImageFolderPath);
  
  $LoopCount = 0;
  
  while (false !== ($File = readdir($Folder))) {
    $FullImagePath = "$ImageFolderPath$File";
  
    echo "$FullImagePath\n";

CreateImage($FullImagePath, $LoopCount);

$LoopCount++;
  }
  closedir($Folder);
?>

 

 

Add error_reporting(E_ALL); to the top while testing, and if that still doesn't put out an error, check your webserver's error log.

 

I suspect you'll find the script is timing out.

 

If you see an error about execution time or something, try adding set_time_limit(0) to the top of your script so that the script can run as long as it needs to.

lol..that is what I am looking into right now.  I tripled the amount of memory already.  I will try to put that at the beginning of the code and see what happens.

 

UPDATE: I changed the php.ini to allow more script executing time and it did complete more photos.  Actually finished all 105 of them.  I am going to try putting the settings back to defaults and then try the code you gave me.

Yep, that worked.  Thanks.  Learned a little more about PHP today!  8)

 

Quick question though.  Is my script right, is there anything I can do for it use less memory?

 

Here is my up-to-date script:

<?php
  set_time_limit(0);

  function CreateImage($ImagePath, $Count) {
  	$image = imagecreatefromjpeg($ImagePath);
    
$width = imagesx($image);
    $height = imagesy($image);
			  
    $new_width = 300;
    $new_height = 300;
			  
    $image_resized = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
			  			  
    $Destination = "test/$Count.jpg";
			  
    imagejpeg($image_resized, $Destination, 100);
imagedestroy($image_resized);
imagedestroy($image);
  }


  $ImageFolderPath = "/home/photoshop/rawphotos/";

  $Folder = opendir($ImageFolderPath);
  
  $LoopCount = 0;
  
  while (false != ($File = readdir($Folder))) {
    $FullImagePath = "$ImageFolderPath$File";
  
if ($FullImagePath != "$ImageFolderPath..") {
  CreateImage($FullImagePath, $LoopCount);
      echo "$FullImagePath\n";
}

$LoopCount++;
  }
  closedir($Folder);
  
  echo "$LoopCount";
?>

 

How big is your biggest picture?

 

When the function execution ends, the memory held by the GD things should be realeased, but during the function call, the GD stuff is of course going to takea bit of memory if it's large images.

Ok, say I compress a 5MB photo.  The function will use ~5MB of memory but once the function is complete, everything that was held in the memory during that function will be erased then it will move on to the next photo.  Therefore, if every file is 5MB the script will only ever use a max of roughly 5MB?  I did add the:

imagedestroy()
function at the end of the function, was that needed or does that help in any way?

 

 

BTW, the photos are going to be coming from a 12 Megapixel camera and a 6 Megapixel camera.  I have the PHP memory limit set to 64MB.

I'm not quite sure whether or not when a function ends if the memory tied up by those resources is released.  I would imagine it is, but let me test it really quick.

 

 

EDIT

 

<?php

function test() {
$a = str_repeat("aaaaaaaaaa", 100000);
echo memory_get_usage() . PHP_EOL;
return;
}
echo memory_get_usage() . PHP_EOL;
test();
echo memory_get_usage() . PHP_EOL;

?>

 

This out puts (for me):

 

51456

1051808

51792

 

That shows me that the resources are indeed freed.

(Grrr couldn't edit my other post)

 

To answer your question:

 

Ok, say I compress a 5MB photo.  The function will use ~5MB of memory but once the function is complete, everything that was held in the memory during that function will be erased then it will move on to the next photo.  Therefore, if every file is 5MB the script will only ever use a max of roughly 5MB?  I did add the:

 

That is correct.

Ok that is awesome...thanks a billion!  So did I need those imagedestroy() commands there do you think or should I leave them there just for good measure?

 

Also, did realize that php had a function to get the memory usage..do you know if that is in bytes or KB?  I will use that t/m and see exactly how much the script is actually taking up.

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.