Search code examples
phpthumbnailstemporary-files

Creat temporay thumb image in PHP


Hey, i am looking for a method to creat temporay thumb file in PHP. Is there any way not to store the image on the server or delete them right after. What I am looking for is a solution like this: http://www.dig2go.com/index.php?shopbilde=772&type=1&size=120

Could some one explain to me how the php code behind this works? For the moment are am using a php code I found on the net for creation of thumb nails:

function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight,    $saveNameAndPath)
{
   if(!file_exists($pathToImage))
   return false;

   else
   {
      //Load image and size
      $img = imagecreatefromjpeg($pathToImage);
      $width = imagesx($img);
      $height = imagesy($img);

      //Calculate the size of thumb
      $new_width = $thumbWidth;
      $new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));

      //Make the new thumb
      $tmp_img = imagecreatetruecolor($new_width, $new_height);

      //Copy the old image and calculate the size
      imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

      //Save the thumb to jpg 
      imagejpeg($tmp_img, $saveNameAndPath);

      return true;
    }
}

Solution

  • function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight, $saveNameAndPath) { if(!file_exists($pathToImage)) return false;

       else
       {
          //Load image and size
          $img = imagecreatefromjpeg($pathToImage);
          $width = imagesx($img);
          $height = imagesy($img);
    
          //Calculate the size of thumb
          $new_width = $thumbWidth;
          $new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));
    
          //Make the new thumb
          $tmp_img = imagecreatetruecolor($new_width, $new_height);
    
          //Copy the old image and calculate the size
          imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
          // sets the header and creates the temporary thumbnail
          // ideal for ajax requests / <img> elements
          header("Content-type: image/jpeg");
          imagejpeg($img);
    
          return true;
        }
    }