GD多重图像操作,内存错误如何处理?
我在 mysql 数据库中有一些图像存储为 BLOB。
图像的最大文件大小为 5MB。
我正在尝试使用 php GD 创建图像的缩略图。
我正在使用下面的脚本:
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;
}
我得到的是:
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
这意味着我的脚本针对一张图像运行,并在运行下一张图像之前终止。
我认为我在每次循环执行后取消设置/销毁我的数据时做错了。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 ini_set('memory_limit','128M'); 来 ini_set 当前脚本运行时允许的内存大小; 但我有一种感觉,那里可能隐藏着更深层次的东西。
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.虽然增加脚本可用的内存并不是坏主意,而且确实可能是需要的,但我想指出您只是销毁了 $uploaded_image。你还应该销毁 $thumb
也就是说,我自己使用 imagedestroy() 经历了奇怪的事情,我不确定它是否完成了预期的所有工作。请参阅我关于 imagedestroy 的问题。不过,它似乎对循环有帮助,所以尝试一下。
While increasing memory available to the script isn't bad idea and indeed may be needed, I'd like to point out you are destroying only $uploaded_image. You should also destroy $thumb
That said, I experienced weird things with imagedestroy() myself and I'm not that sure it does all the job it's expected to. See my question on imagedestroy. Still, it seems to help in loops, so try it.