A simple way to create a compression diagram in PHP _php tips

Source: Internet
Author: User
Tags php database

This article illustrates a simple way to create a compression diagram in PHP. Share to everyone for your reference, specific as follows:

<?php//Create compression graph function _create_thumbnail ($srcFile, $toW, $toH, $toFile = "") {if ($toFile = = "") {$toFile = $s
  Rcfile;
  } $info = "";
  $data = getimagesize ($srcFile, $info);
  if (! $data) return false;
      Load the file into the resource variable im switch ($data [2]) {Case 1: $im = Imagecreatefromgif ($srcFile);
    Break
      Case 2: $im = Imagecreatefromjpeg ($srcFile);
    Break
      Case 3: $im = Imagecreatefrompng ($srcFile);
  Break
  ///compute the width and height of the thumbnail $srcW = Imagesx ($im);
  $srcH = Imagesy ($im);
  $toWH = $toW/$toH;
  $srcWH = $srcW/$srcH;
    if ($toWH <= $srcWH) {$ftoW = $toW;
  $ftoH = (int) ($ftoW * ($srcH/$srcW));
    else {$ftoH = $toH;
  $ftoW = (int) ($ftoH * ($srcW/$srcH)); } if (Function_exists ("Imagecreatetruecolor")) {$ni = Imagecreatetruecolor ($ftoW, $ftoH);//Create a new True color image if ($ni
    {//resampling copies of partial images and resizing to maintain better sharpness imagecopyresampled ($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } ELSE {//Copy part of image and resize $ni = Imagecreate ($ftoW, $ftoH);
    Imagecopyresized ($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
    } else {$ni = Imagecreate ($ftoW, $ftoH);
  Imagecopyresized ($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); //Save to file Unified for. png format imagepng ($ni, $toFile);
  Output the image to the browser or file Imagedestroy ($ni) in PNG format;
Imagedestroy ($im);

 }?>

For more information on PHP related content readers can view the site topics: "PHP graphics and picture Operation skills Summary", "PHP array of Operations Skills encyclopedia", "PHP Mathematical Calculation Skills Summary", "PHP date and Time usage summary", "PHP object-oriented Programming Program", Summary of PHP string usage, Getting Started tutorial on Php+mysql database operations, and summary of common PHP database operations techniques

I hope this article will help you with the PHP program design.

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.