I have a script that uploads images and rotates them depending on the orientation and I'm experiencing the problem that when an image that has EXIF tags is uploaded, I get an error saying:
Allowed memory size of 33554432 bytes exhausted (tried to allocate 10368 bytes.
And then the line it is referring to in the error log.
I did notice that it's only happening with images that have EXIF tags. If normal images, generated by Photoshop or something are uploaded, it works without problems.
The actual image orientation code is the following:
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
}
The exact line in the error_log where the memory problem happens is actually the one where it says:
$img = imagerotate($img, $deg, 0);
The way I am calling it in the script is the following:
$dirname = session::value('user_id');
$rotatedfile = '/home/myfolder/public_html/'.$dirname.'/'.$file_name;
$rotatedfile = $this->correctImageOrientation($rotatedfile);
What I am basically trying to achieve is that the rotated image gets saved in the same place as the original file, basically replacing it.
Again, this is ONLY happening with images that contain EXIF information. All others are uploaded without problems.
What could be causing this memory allocation problem?