0

Okay, I'm going to preface this with the disclaimer that I have no code that I've tried, primarily because I can't find anything or figure out anything that might work in this particular case. Be gentle with the downvotes lol

I have an application that uses the Cropit image cropping plugin. This passes an image back to my controller in Base64 format where I process it and save it. The issue is with some images the save is quite lengthy, 6-20 seconds. Long enough that a user might think the application has hung up.

So my client thought a progress bar would be nice, which I agree it's better than a spinner or just making the user wait. The problem is I don't know how to get the progress of the save, or if I even can. If it were an iterative process it would be easy, but since this is just one event that as far as I can tell doesn't return any sort of progress I'm baffled as to how to do this.

This is the code that is running for so long (ps any tips on how to speed this process are welcome as well!) The script does some resizing and other database functions, but I've done some benchmarking and narrowed it down to the actual save function. For the record the previous iteration of this code did things a little differently and used file_put_contents. The time result was exactly the same.

    if(imagejpeg($image_p,$new_dir.$postData['page_image'],$compression)) {
      $return['updated'] = "updated";
      $return['image_src'] = 'assets/dist/img/user_images/'.$page.'/'.$postData['page_image'];
      $return['image_name'] = $postData['page_image'];
      $this->clean_up_images($page);
    } else {
      $return['updated'] = "error";
    }

Oddly (to me at least) images that are complex take much longer to save, regardless of actual file size. So for example a photo of a letterhead that is 3mb will take 200-300 milliseconds, a photo of a bunch of people that is only 1.7mb will take 5-6 seconds.

Help?

Rick Calder
  • 18,310
  • 3
  • 24
  • 41
  • 1
    How about to move the resize part in to an `cronjob` that ferquently (each 1min) resizes new images in the background. – JOUM Nov 30 '16 at 23:02
  • Unfortunately it's part of a user based registration system. It only happens if the user changes their background image, but it's inline to their actual registration process. Thank you for the thought though, in any other situation that would be fine. – Rick Calder Nov 30 '16 at 23:06
  • @RickCalder there is no point for the user to wait until you fully compress the image. JOUM is right, you need to do this process as a cronjob. imagejpeg I assume is the function which takes almost all the processing time. Unless that particular function offers progress updates, I guess there's not much you can do. – BitParser Nov 30 '16 at 23:16
  • The file is passed to PHP via a form element that sends a Base64 string. The image is then created via imagecreatefromstring. The file size does change when converted to jpeg and the dimensions are being set to a max of 2560 wide via imagecopyresample. Image manipulation isn't one of my strong points, done a lot of Googling on this! – Rick Calder Nov 30 '16 at 23:17
  • @TGO to further explain the process, the user is creating their own page on the server. During the process they can preview their page. This process requires the image they've uploaded to be saved to the server, it can't be done via a cronjob in this case. Unless there is something I've misunderstood – Rick Calder Nov 30 '16 at 23:20
  • 1
    Sorry Rick, I was confusing `imagejpeg()` with a different function, but in essence you need a new thread. Perhaps [async include](http://stackoverflow.com/questions/124462/asynchronous-php-calls) with a combination of [this](http://stackoverflow.com/a/1206909/4982088)? Having the stream size available you might be able to calculate percentage steps. – Xorifelse Nov 30 '16 at 23:21
  • ooh, that's promising @Xorifelse in another earlier iteration I was using output buffering to create the image to save. I'll definitely take a shot at working with that. Thank you! – Rick Calder Nov 30 '16 at 23:24

0 Answers0