issue with image_auto_rotate

See all posts Reply

issue with image_auto_rotate new!
by jon, 4 years, 4 months ago
Great Coding and class, i use this upload class lots.
I love the idea of the new feature image_auto_rotate, but am having the below issues with it

I have tried several different images (taken on a Samsung s7), but all fail with a server time out error (maximum execution time).
If i add $handle->image_auto_rotate = false; all then works well, but does not rotate the image (if it needs rotating)

Below is the code i am using:

//** ============ images sizes =============== 
// small thumb
$imageSize_arr[] = array('image_resize' => true, 'image_x' => THUMBNAILWidth, 'image_ratio_y' => true, 'jpeg_quality' => 80, 'image_unsharp_amount' => 80, 'file_name_body_add' => '_thumb');
// normal thumb
$imageSize_arr[] = array('image_resize' => true, 'image_x' => THUMBNAILWidthLARGE, 'image_ratio_y' => true, 'jpeg_quality' => 80 , 'image_unsharp_amount' => 80, 'file_name_body_add' => '_largethumb');
// full size image 
$imageSize_arr[] = array('image_resize' => true, 'image_x' => IMAGEWidth, 'image_ratio_y' => true, 'jpeg_quality' => 80, 'image_unsharp_amount' => 80);
//origional file
$imageSize_arr[] = array('image_resize' => false, 'file_name_body_add' => '_origional');

$handle = new upload($file); // initiate upload class
$fileName = urlencode(md5($handle->file_src_name.uniqid(true).time())); // genrate a unique file name

if ($handle->uploaded) {
  foreach ($imageSize_arr as $size_arr){ 
    $handle->file_new_name_body   = $fileName; 
    $handle->file_overwrite 	        = true;
    $handle->file_max_size		= MAX_BYTE_IMAGE_SIZE;					
    $handle->allowed 			= array('image/*');
    //$handle->image_auto_rotate   = false;
    foreach($size_arr as $key=>$value){
      $handle->$key = $value;
    }											
    $handle->process(REVIEW_GALLERY_FULL_IMAGE_PATH.$reviewID.'/');
  }				
  // check if all went well
  if ($handle->processed) {
    $handle->clean();						
  }
}

Any help would be much appreciated.
(i think how it rotates needs looking at as it loops for too long if the images being uploaded is large (the images i am testing with are around 8MB))

Thanks
JonReply
Re: issue with image_auto_rotate new!
by jon, 4 years, 4 months ago
PS only fair i make a donation, as i do use your code quit a bit :-) (love how neat it makes uploading files).
small donation sent...
Thank you again...Reply
Re: issue with image_auto_rotate new!
by colin, 4 years, 4 months ago
You are processing too many images in one request, and your images are too large. So the script times out or runs out of memory. And it does so only when it needs to rotate the image, so it is very likely a memory issue. You can check that in your server logs.

Consider resizing your images outside of the upload request, in a queue, processed maybe with a cron.Reply
Re: issue with image_auto_rotate new!
by jon, 4 years, 4 months ago
Thank you for your help

I have reordered my code to first process the main image (only 1 image), store it and then create the (required by me) resized images (with a new instance of class.upload), this works better as it only needs to rotate the main image so seems to be saving in execution time (and possibly memory too). I get no errors with this, but essentially the same results :-) (of multiple images and auto-rotated!).

The error i am getting (before i re-ordered with the above) was
"Fatal error: Maximum execution time of 30 seconds exceeded in /home/site/public_html/class/upload.class.php on line 3527"

I don't think this is a memory issue.

I have looked at your upload class code (nice, but confusing to me :-))
I am of course naive towards your code and its complexity, but i wonder if moving the rotation until after the re-sizing would speed up the script (as my logic says it is faster and less memory to rotate a smaller file size/image, although i could of course be wrong).

I have tried to rearrange the code by adding in a "pre-auto-rotate" that sets the "image_src_y & image_src_x" measurements only (as if the image was rotated), then resizes and then the auto-rotate.
This seems to work faster, but due to my lack of understanding, is getting some unexpected results.... :-)
Would placing the auto-rotate after the resize be an idea, or would it make no difference in the execution time/memory anyhow?
If you think this will work, i am happy to spend some time understanding your upload class, to try and make this work correct.

Thanks Again
JonReply
Re: issue with image_auto_rotate new!
by jon, 4 years, 4 months ago
Hello again Colin

Just a quick follow up.
Indeed, i have managed to change the order of auto-flip image & auto-rotate image
to after the resize (only needed to change image_src_? to image_dst_? and place the code after "if ($this->image_resize) {...}").
This works MUCH faster and with multiple images (before 1 image was taking > 20 seconds, now 3 are taking < 10).

Will this have broken anything else (like crop, etc. although these seem to use image_dst too)?

Thanks Again
JonReply
Re: issue with image_auto_rotate new!
by colin, 4 years, 4 months ago
It is a good idea, and it works in your case.

But the auto-flip and auto-rotate must happen before any image processing occurs. Besides, resizing dimensions may be based on the source image dimensions, but the source image has to be rotated first.Reply