PHP Simple thumbnail class |image.class.php

Source: Internet
Author: User
Tags imagejpeg

How to use:


$img = new Iamge;
$img->resize (' dstimg.jpg ', ' srcimg.jpg ', 300, 400);
Description: This is proportional scaling, dstimg.jpg is the target file, Srcimg.jpg is the source file, the following is the target file width and height
$img->thumb (' dstimg.jpg ', ' scrimg.jpg ', 300, 300);
Description: This is in accordance with proportional thumbnails, such as commonly used in the avatar thumbnail, dstimg.jpg is the target file, Srcimg.jpg is the source file, the following is the target file width and height
This is for GD Coucay such trouble, if the use of Imagick words, only need two functions on the implementation of the above function, to check the document to find.

<?php
Class image{
 
 public function Resize ($dstImg, $srcImg, $dstW, $dstH) {
 & Nbsp;list ($srcW, $srcH) = getimagesize ($SRCIMG);
   $scale = min ($dstW/$srcW, $dstH/$srcH);
        $newW = Round ($srcW * $scale);
        $newH = Round ($srcH * $scale);
   $newImg = Imagecreatetruecolor ($newW, $newH);
   $srcImg = Imagecreatefromjpeg ($SRCIMG);
  imagecopyresampled ($newImg, $srcImg, 0, 0, 0, 0, $newW, $newH, $srcW, $srcH);
  imagejpeg ($NEWIMG, $DSTIMG);
 }
 
 public function Thumb ($dstImg, $srcImg, $dstW, $dstH) {
  list ($srcW, $srcH) = Getimag Esize ($SRCIMG);
   $scale = max ($dstW/$srcW, $dstH/$srcH);
   $newW = round ($dstW/$scale);
   $newH = round ($dstH/$scale);
   $x = ($srcW-$newW)/2;
   $y = ($srcH-$NEWH)/2;
   $newImg = Imagecreatetruecolor ($dstW, $dstH);
   $srcImg = Imagecreatefromjpeg ($SRCIMG);
  imagecopyresampled ($newImg, $srcImg, 0, 0, $x, $y, $dstW, $dstH, $newW, $newH);
  imagejpeg ($NEWIMG, $DSTIMG);
 }
  
}

Function Createfromtype ($type, $srcImg) {
  $function = "Imagecreatefrom$type";
 return $function ( $SRCIMG);
}
//To resolve problems with different picture formats

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.