The Introduction to PHP tutorial on image processing skills analysis _php Skills

Source: Internet
Author: User
Tags imagejpeg php tutorial

This example describes the PHP image processing. Share to everyone for your reference, specific as follows:

demo1.php

<?php
  //general generated images can be png,jpg,gif,bmp
  //jpeg,wbmp
  //First step, set file MIME type, output type text/html type is Web page type, default can not write
  Changing output type to Image stream
  header (' content-type:image/png; ');
  The second step, create a graphics area, image background
  //There are two ways to create, resource type, generally add @ symbol, prevent error
  //imagecreatetruecolor returns a resource handle//
  This function creates an image area , the background is black
  $im = Imagecreatetruecolor (200,200) when there is no padding;
  The third step, has the blank image area, draws the color, the text is called, the line ah ...
  //Fill color change, first have a color filler
  //imagecolorallocate--For an image distribution color
  $blue = imagecolorallocate ($im, 0,102,255);
  Fill the blue color onto the background
  //imagefill--area fill
  imagefill ($im, 0,0, $blue);
  Fourth, in the background of the blue input some lines, text and other
  $white = Imagecolorallocate ($im, 255,255,255);
  Imageline--Draw a line segment
  imageline ($im, 0,0,200,200, $white);
  Imageline ($im, 200,0,0,200, $white);
  Imagestring-horizontally draw a line of strings
  imagestring ($im, 5,80,20, ' Mr.one ', $white);
  The fifth step, output final graphics
  //in PNG format to output the image to the browser or file
  imagepng ($im);
  The sixth step, I want all the resources empty
  Imagedestroy ($im);
? >

demo2.php

<?php
  //SRC can insert all kinds of pictures
  //demo1.php is actually a PNG picture
  header (' content-type:text/html; Charset=gbk ');
  Echo ' 

demo3.php

<?php
  //Simple Verification Code
  //random number//
  Why should the number of loops between 0-15?
  //Because to implement the simplest alphanumeric and digital blending
  ///16 0-9 a-f
  //dechex-decimal conversion to hexadecimal
  //create a four-bit captcha for
  ($i =0; $i <4;$ i++) {
    $nmsg. = Dechex (Mt_rand (0,15));
  }
  echo $nmsg;
  Header (' content-type:image/png; ');
  $im = Imagecreatetruecolor (75,25);
  $blue = Imagecolorallocate ($im, 0,102,255);
  $white = Imagecolorallocate ($im, 255,255,255);
  Imagefill ($im, 0,0, $blue);
  Imagestring ($im, 5,20,5, $nmsg, $white);
  Imagepng ($im);
  Imagedestroy ($im);
? >

demo4.php

<?php
  define (' __dir__ ', DirName (__file__). \\');
  Load an existing image
  header (' content-type:image/png; ');
  Header (' content-type:image/jpeg; ');
  Imagecreatefrompng--Create a new image from a PNG file or URL//image to
  load images, can edit the image
  //In the loaded image, add a small watermark
  $im = Imagecreatefrompng (__dir__. ') Ss.png ');
  $im = Imagecreatefromjpeg (' xx.jpg ');
  $white = Imagecolorallocate ($im, 255,255,255);
  Imagestring ($im, 5,10,10, ' http://www.oneStopWeb.cn ', $white);
  Imagepng ($im);
  Imagejpeg ($im);
  Imagedestroy ($im);
? >

demo5.php

<?php
  define (' __dir__ ', DirName (__file__). \\');
  Load an existing image
  header (' content-type:image/png; ');
  Header (' content-type:image/jpeg; ');
  Imagecreatefrompng--Create a new image from a PNG file or URL//image to
  load images, can edit the image
  //In the loaded image, add a small watermark
  $im = Imagecreatefrompng (__dir__. ') Ss.png ');
  $im = Imagecreatefromjpeg (' xx.jpg ');
  $white = Imagecolorallocate ($im, 255,255,255);
  Imagestring ($im, 5,10,10, ' http://www.oneStopWeb.cn ', $white);
  Font fonts must also support Chinese
  $font = ' C:\WINDOWS\Fonts\SIMHEI. TTF ';
  Font file
  $text = iconv (' GBK ', ' utf-8 ', ' read who asked to chant ');
  Using the system-supplied font
  //second parameter, is the font size, the third parameter is the rotation angle, the 4,5 parameter is coordinates
  imagettftext ($im, 20,10,50,100, $white, $font, $text);
  Imagepng ($im);
  Imagejpeg ($im);
  Imagedestroy ($im);
? >

demo6.php

 <?php/thumbnail, not only the size of the surface has changed, the volume has also changed//is really changed, not the surface of the shrinking define (' __dir__ ', dirname (__file__ ).'
  \\');
  Header (' content-type:image/png; '); GetImageSize-Get image size//Get the length and height of the original artwork list ($width, $height) = getimagesize (__dir__. ')
  Ss.png ');
  Scale the original image into 40% $_width = $width * 0.4;
  $_height = $height * 0.4;
  Create a new Diagram $im = Imagecreatetruecolor ($_width,$_height); The work below is to load the original artwork, copy the original image to the new diagram//load the original $_im = Imagecreatefrompng (__dir__. ')
  Ss.png '); Resample the original image, copy it to the new diagram, and finally press 0.4 to output the//imagecopyresampled-resampling the copied part of the image and resizing imagecopyresampled ($im, $_im,0,0,0,0,$_width,$_
  Height, $width, $height);
  Output Imagepng ($im) of the new graph;
  The second parameter is not required, direct null over//third parameter, is 0-100 to adjust the sharpness of JPG//If it is imagepng, then all are HD//imagejpeg ($im, null,50);
  Destruction of Imagedestroy ($im); Imagedestroy ($_im);?> 

For more information on PHP related content readers can view the site topics: "PHP graphics and pictures Operating skills summary", "Basic PHP Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", "PHP array (Array) Operations tips Encyclopedia, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP Common database Operation tips Summary"

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.