Jump to content

imagedestroy() in a loop?


TheBG

Recommended Posts

I am attempting to convert 255 .jpg images in a directory to thumbnails and I don't want to do it with Photoshop !!  This code works great for the first 21 images (800 x 1300 roughly) and loads the 22nd before it dies with No warnings, just stop. I am sure it is a memory leak type of deal even though I have destroyed both images during each itteration.

 

The only difference between this code and all the others is that I am processing a large number of images. All I can think of is using imagedestroy in a loop? Safe Mode?

 

I'll take all the help I can get...

 

<?php

//***********************************************************************

// Power Thumb

//

// Define Constants ::

//

// PHOTO_PATH :: the relative path to photo dircetory ( no final / )

// THUMB_PATH :: relative path to thumbnails directory ( no final / )

// T_LONG :: length of long side of thumbnail in pixels

// T_SHORT :: length of short side of thumbnail in pixels

// FILE_MASK :: just like any search - eg  *.jpg

//

//***********************************************************************

define( "PHOTO_PATH", "path/to/photo/directory");

define( "THUMB_PATH", "path/to/thumbnail/directory");

define("T_LONG", 226);

define("T_SHORT", 150);

define("FILE_MASK", "*.jpg");

//***********************************************************************

//

// Do NOT edit below here

//

//***********************************************************************

// Check to see if PHOTO_PATH is valid

if( ! is_dir( PHOTO_PATH)) {

print PHOTO_PATH . " is not a valid Directory.";

die;

}

// Check to see if THUMB_PATH is valid

if( ! is_dir( THUMB_PATH)) {

print THUMB_PATH . " is not a valid Directory.";

// Create it

mkdir( THUMB_PATH, 0755);

print " - Created -<br />";

}

// Get an array of file names to convert

$orig_file_names = glob( PHOTO_PATH . "/" . FILE_MASK);

 

foreach($orig_file_names as $val) {

echo "$val";

// Do Function - **imagedestroy in loop work around**

if( thumb($val)) {

print " :: converted<br />";

}

}

 

function thumb($file) {

//open the photo

$src_photo = imagecreatefromjpeg($file);

// Get photo dimensions

$p_width = imagesx($src_photo);

$p_height = imagesy($src_photo);

// Determine format

if( $p_width >= $p_height) {

// Horizontal

$t_width = T_LONG;

$t_height = T_SHORT;

} else {

// Vertical

$t_width = T_SHORT;

$t_height = T_LONG;

}

// Open destination photo

$dest_photo = imagecreatetruecolor($t_width, $t_height);

print " I've created another one!";

// Resize

imagecopyresampled($dest_photo, $src_photo, 0, 0, 0, 0, $t_width, $t_height, $p_width, $p_height);

print " I've resampled ";

// Save to file

imagejpeg($dest_photo, THUMB_PATH . "/t_" . basename($file), 100);

// Free resources

imagedestroy($dest_photo);

imagedestroy($src_photo);

return TRUE;

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/44958-imagedestroy-in-a-loop/
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.