Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making script preformance more efficient

Tags:

jquery

php

I have a script that makes thumbnails of the pictures in a directory. But it's execution takes too long (about 170 images in the directory).

The script is called by ajax request. After 70% completion, I receive an error probably due to time out (takes about 3-4 minutes).

How can I solve this issue?

function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) 
{
 // open the directory
 $dir = opendir( $pathToImages );

 // loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' ) 
{

  // load image and get image size
  $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
  $width = imagesx( $img );
  $height = imagesy( $img );

  // calculate thumbnail size
  $new_width = $thumbWidth;
  $new_height = floor( $height * ( $thumbWidth / $width ) );

  // create a new temporary image
  $tmp_img = imagecreatetruecolor( $new_width, $new_height );

  // copy and resize old image into new image 
  imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

  // save thumbnail into a file
   imagejpeg( $tmp_img, "{$pathToThumbs}thumb_{$fname}" );
   }
  }
   // close the directory
  closedir( $dir );
  }

  createThumbs($directory,$directory."/thumbs/",150);

ajax call;

   var ajaxr=$.ajax({
  type: "POST",
  url: "after_upload.php",
  timeout:600,
  beforeSend: function() {
  $("#result").html('<div align="center"><h2>מבצע עיבוד נתונים יקח זמן ,חכה..תכין קפה בנתיים      ותעשן סיגריה</h2><div><img src="loader.gif"/><div dir="rtl" style="margin:15px;">טוען מידע וממיר תמונות... <button id="cancel" style="padding:5px;">בטל פעולה ותחזור חזרה [X]</button></div></div>  </div>');
                        },
  success: function(data){
       $("#result").html(data);
  },
  error: function(xhr, textStatus, errorThrown) {
                             $("#result").html(textStatus);
                        }
  });

now,increased the time out to 3000 in ajax call and it instantly,for some reason return timeout error.if i remove time out property from the call ..it preforms the call and script executes..but only 70% of job is done..done returned empty error...

UPDATE:..i preformed everythin to make script execution time better now:console return 404 Not Found..

like image 249
user2268106 Avatar asked May 07 '13 08:05

user2268106


People also ask

How do you reduce the time of script evaluation?

Compressing scripts is the process of reducing the file size by encrypting JavaScript files. Compressing makes the scripts take less space in web servers and less time to transfer files to your other servers. Cloudflare help speeds up JavaScript evaluation time by compressing scripts using Brotli or Gzip compression.


1 Answers

Make the creation of the thumbnail run in a loop and after every loop remove the previous resource from the server memory.

imagedestroy($thumb);
imagedestroy($source);

This would greatly help, I just finished something very similar.

like image 62
James Okpe George Avatar answered Sep 21 '22 06:09

James Okpe George