I have some images in a mysql database stored as BLOB’s.
The maximum file size of the images is 5MB.
I am trying to create thumbnails of the images, using php GD.
I am using the script below:
include 'models/connectdb.php';
// ------ starting up ------ //
$directory = 'views/images/generated/events/xs/';
$table = 'events';
$dimension_x = 50;
$dimension_y = 50;
// ------- proccess ------- //
//$query = "ALTER TABLE $table ADD thumb0 VARCHAR(100);";
//$result = mysql_query($query) or die(mysql_error());
$query = "SELECT * FROM $table;";
$result = mysql_query( $query ) or die(mysql_error());
while( $row = mysql_fetch_array( $result ) ) {
$id = $row['id'];
$uploaded_image = base64_decode( $row['image'] );
$uploaded_image = imagecreatefromstring( $uploaded_image );
$uploaded_x = imagesx($uploaded_image);
$uploaded_y = imagesy($uploaded_image);
do{
$filename = random_32();
$filename = $directory.$filename.'.jpg';
} while( file_exists($filename) );
$thumb = imagecreatetruecolor( $dimension_x , $dimension_y );
imagecopyresampled( $thumb , $uploaded_image , 0 , 0 , 0 , 0 , $dimension_x , $dimension_y , $uploaded_x, $uploaded_y);
imagedestroy( $uploaded_image ); // I also tried unset( $uploaded_image );
imagejpeg( $thumb , $filename , 90);
$query = "UPDATE $table SET image_xs = '$filename' WHERE id = $id LIMIT 1;";
mysql_query($query) or die(mysql_error());
echo "ran for one , ";
}
echo "it all ran";
function random_32() { // this function generates a random filename
$filename = rand( 100000000000000000 , 999999999999999999 );
$filename = md5( $filename );
return $filename;
}
What I am getting is:
ran for one ,
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 14032 bytes) in /home/leagu11/public_html/nightscene.gr/create_thumbs_people.php on line 21
Meaning my script runs for one image and dies before running for the next one.
I think I am doing something wrong with unsetting / destroying my data after each loop execution.
Any ideas?
You could ini_set the allowed memory size for the runtime of the current script using
ini_set('memory_limit','128M');But i have a feeling there might be something deeper hiding there.