【发布时间】:2014-08-11 19:33:52
【问题描述】:
我有一个脚本可以上传图像并根据方向旋转它们,我遇到的问题是,当上传带有 EXIF 标签的图像时,我收到一条错误消息:
允许的内存大小为 33554432 字节已用尽(试图分配 10368 字节。
然后是它在错误日志中引用的行。
我确实注意到它只发生在带有 EXIF 标签的图像上。如果上传由 Photoshop 生成的普通图像或其他东西,则可以正常工作。
实际的图像方向代码如下:
function correctImageOrientation($fullpath) {
if (function_exists('exif_read_data')) {
$exif = exif_read_data($fullpath);
if($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if($orientation != 1){
$img = imagecreatefromjpeg($fullpath);
$deg = 0;
switch ($orientation) {
case 3:
$deg = 180;
break;
case 6:
$deg = 270;
break;
case 8:
$deg = 90;
break;
}
if ($deg) {
$img = imagerotate($img, $deg, 0);
}
// then rewrite the rotated image back to the disk as $filename
imagejpeg($img, $fullpath, 100);
} // if there is some rotation necessary
} // if have the exif orientation info
} // if function exists
}
error_log 中发生内存问题的确切行实际上是它所说的那一行:
$img = imagerotate($img, $deg, 0);
我在脚本中的调用方式如下:
$dirname = session::value('user_id');
$rotatedfile = '/home/myfolder/public_html/'.$dirname.'/'.$file_name;
$rotatedfile = $this->correctImageOrientation($rotatedfile);
我基本上想要实现的是旋转后的图像保存在与原始文件相同的位置,基本上是替换它。
同样,这只发生在包含 EXIF 信息的图像上。其他所有上传都没有问题。
什么可能导致这个内存分配问题?
【问题讨论】: