Advertisement
Virajsinh

Create a Thumbnail Image in PHP Using Library

Apr 21st, 2023 (edited)
996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | Source Code | 0 0
  1. <?php
  2. // https://code.tutsplus.com/tutorials/how-to-create-a-thumbnail-image-in-php--cms-36421
  3. // File Name : thumbimage.class.php
  4. class ThumbImage
  5. {
  6.     private $source;
  7.     public function __construct($sourceImagePath)
  8.     {
  9.         $this->source = $sourceImagePath;
  10.     }
  11.     public function createThumb($destImagePath, $thumbWidth=100)
  12.     {
  13.         $sourceImage = imagecreatefromjpeg($this->source);
  14.         $orgWidth = imagesx($sourceImage);
  15.         $orgHeight = imagesy($sourceImage);
  16.         $thumbHeight = floor($orgHeight * ($thumbWidth / $orgWidth));
  17.         $destImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
  18.         imagecopyresampled($destImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $orgWidth, $orgHeight);
  19.         imagejpeg($destImage, $destImagePath);
  20.         imagedestroy($sourceImage);
  21.         imagedestroy($destImage);
  22.     }
  23. }
  24. ?>
  25.  
  26. <?php
  27.     // Another File
  28.     require "thumbimage.class.php";
  29.     $objThumbImage = new ThumbImage("/web/uploads/orig.jpg");
  30.     $objThumbImage->createThumb("/web/uploads/thumb.jpg", 125);
  31. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement