上传/调整图像大小时发生 PHP 致命错误
我使用的代码将上传图像,将图像放入“调整大小”文件夹中,调整图像大小,将图像移动到另一个文件夹中,然后从“调整大小”文件夹中删除图像,但是我得到了以下错误:
“致命错误:第 649 行 /home/photogra/public_html/administrator/components/com_gallery/admin.gallery.php 中允许的内存大小 33554432 字节已耗尽(尝试分配 14172 字节) “
图像甚至都不大! (例如265kb)
这是我正在使用的代码(带有行号):
635 move_uploaded_file($_FILES['image']['tmp_name'],$mainframe->getCfg( 'absolute_path' ) ."/virtualgallery/images/resize/$newname");
636
637 /* resize images - width 600px */
638 $docRoot = $GLOBALS['mosConfig_absolute_path'];
639 $pathToImages = $docRoot.'/virtualgallery/images/resize/';
640 $pathToThumbs = $docRoot.'/virtualgallery/images/';
641 $thumbHeight = 600;
642
643 $dir = opendir( $pathToImages );
644 while (false !== ($fname = readdir( $dir ))) {
645 $info = pathinfo($pathToImages . $fname);
646 if ( strtolower($info['extension']) == 'jpg' ) {
647 $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
648 $width = imagesx( $img );
649 $height = imagesy( $img );
650 $new_width = floor( $width * ( $thumbHeight / $height ) );
651 $new_height = $thumbHeight;
652 $tmp_img = imagecreatetruecolor( $new_width, $new_height );
653 imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
654 imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
655 };
656 };
657 closedir( $dir );
658
659 /* delete file(s) from resize folder */
660 $dir = $docRoot.'/virtualgallery/images/resize/';
661 foreach(glob($dir.'*.*') as $v) {
662 unlink($v);
663 };
此外,当我收到该错误时,图像被卡在“调整大小”文件夹中。如果有人可以提供帮助,那就太棒了! :)
I'm using a code which will upload an image, put the image in the "resize" folder, resize the image, move the image into another folder, then delete the image from the "resize" folder, however I'm getting the following error:
"Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 14172 bytes) in /home/photogra/public_html/administrator/components/com_gallery/admin.gallery.php on line 649"
The images aren't even big! (eg. 265kb)
Here's the code I'm using (with the line numbers):
635 move_uploaded_file($_FILES['image']['tmp_name'],$mainframe->getCfg( 'absolute_path' ) ."/virtualgallery/images/resize/$newname");
636
637 /* resize images - width 600px */
638 $docRoot = $GLOBALS['mosConfig_absolute_path'];
639 $pathToImages = $docRoot.'/virtualgallery/images/resize/';
640 $pathToThumbs = $docRoot.'/virtualgallery/images/';
641 $thumbHeight = 600;
642
643 $dir = opendir( $pathToImages );
644 while (false !== ($fname = readdir( $dir ))) {
645 $info = pathinfo($pathToImages . $fname);
646 if ( strtolower($info['extension']) == 'jpg' ) {
647 $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
648 $width = imagesx( $img );
649 $height = imagesy( $img );
650 $new_width = floor( $width * ( $thumbHeight / $height ) );
651 $new_height = $thumbHeight;
652 $tmp_img = imagecreatetruecolor( $new_width, $new_height );
653 imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
654 imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
655 };
656 };
657 closedir( $dir );
658
659 /* delete file(s) from resize folder */
660 $dir = $docRoot.'/virtualgallery/images/resize/';
661 foreach(glob($dir.'*.*') as $v) {
662 unlink($v);
663 };
Also when I get that error, images are getting stuck in the "resize" folder.. If anyone can help, that'd be fantastic! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试调整目录中所有图像的大小,而不在每张图像之后释放内存。 尝试添加
对于初学者来说。 另外,完成图像后立即取消链接,而不是再次迭代该目录。
You're trying to resize all the images in a directory without freeing the memory after each one. Try adding
For starters. Also, unlink the image as soon as you're done with it rather than iterating over the directory a second time.