Summary of PHP image processing function examples, PHP image function example code

WBOY
Release: 2016-07-25 08:51:42
Original
812 people have browsed it
  1. imagecolorsforindex()

  2. imagecolorat()
  3. Draw graphics on the picture $img=imagecreatefromgif("./images/map.gif");

  4. < ;?PHP

  5. /**
  6. * Image sharpening process
  7. * edit by bbs.it-home.org
  8. */
  9. $red= imagecolorallocate($img, 255, 0, 0);

  10. imageline($img, 0, 0, 100, 100 , $red);

  11. imageellipse($img, 200, 100, 100, 100, $red);
  12. imagegif($img, "./images/map2.gif");
  13. imagedestroy($img);
  14. < /p>
Copy code

Normal zoom of pictures

  1. function thumn($background, $width, $height, $newfile) {
  2. list($s_w, $s_h)=getimagesize($background);//Get the original image height and width
  3. if ($ width && ($s_w < $s_h)) {
  4. $width = ($height / $s_h) * $s_w;
  5. } else {
  6. $height = ($width / $s_w) * $s_h;
  7. }
  8. $ new=imagecreatetruecolor($width, $height);
  9. $img=imagecreatefromjpeg($background);
  10. imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h) ;
  11. imagejpeg($new, $newfile);
  12. imagedestroy($new);
  13. imagedestroy($img);
  14. }
  15. thumn("images/hee.jpg", 200, 200, "./images/hee3.jpg ");
Copy code

gif transparent color processing

  1. /**
  2. * Image cropping process
  3. * edit by bbs.it-home.org
  4. */

  5. function cut($background, $cut_x, $cut_y, $cut_width, $cut_height, $location){

  6. $back=imagecreatefromjpeg($background);
  7. $new=imagecreatetruecolor($cut_width, $cut_height);
  8. imagecopyresampled($new, $back, 0, 0, $cut_x, $cut_y, $ cut_width, $cut_height,$cut_width,$cut_height);
  9. imagejpeg($new, $location);
  10. imagedestroy($new);
  11. imagedestroy($back);
  12. }
  13. cut("./images/hee.jpg" , 440, 140, 117, 112, "./images/hee5.jpg");
  14. ?>

Copy code

Image watermark text watermark

  1. /**
  2. *
  3. * Add text watermark to pictures
  4. */

  5. function mark_text($background, $text, $x, $y){

  6. $back=imagecreatefromjpeg($background);
  7. $color=imagecolorallocate($back, 0, 255, 0);
  8. imagettftext($back, 20, 0, $x, $y, $color, "simkai.ttf" , $text);
  9. imagejpeg($back, "./images/hee7.jpg");
  10. imagedestroy($back);
  11. }
  12. mark_text("./images/hee.jpg", "Details on PHP", 150, 250);
  13. //Image watermark
  14. function mark_pic($background, $waterpic, $x, $y){
  15. $back=imagecreatefromjpeg($background);
  16. $water=imagecreatefromgif($waterpic);
  17. $w_w =imagesx($water);
  18. $w_h=imagesy($water);
  19. imagecopy($back, $water, $x, $y, 0, 0, $w_w, $w_h);
  20. imagejpeg($back," ./images/hee8.jpg");
  21. imagedestroy($back);
  22. imagedestroy($water);
  23. }
  24. mark_pic("./images/hee.jpg", "./images/gaolf.gif", 50 , 200);

Copy code

Picture rotation

  1. /**
  2. * Image rotation
  3. * edit by bbs.it-home.org
  4. */
  5. $back=imagecreatefromjpeg("./images/hee.jpg");

  6. imagejpeg($new, "./images/hee9.jpg");
  7. ?>

Copy code

图片水平翻转垂直翻转

  1. /**
  2. * Flip the picture horizontally Flip it vertically
  3. * edit by bbs.it-home.org
  4. */
  5. function turn_y($background, $newfile){

  6. $back=imagecreatefromjpeg($background);

  7. $width=imagesx($back);
  8. $height=imagesy($back);
  9. $new=imagecreatetruecolor($width, $height);
  10. for($x=0; $x < $width; $x++){
  11. imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
  12. }
  13. imagejpeg($new, $newfile);
  14. imagedestroy($back);
  15. imagedestroy($new);
  16. }
  17. function turn_x($background, $newfile){
  18. $back=imagecreatefromjpeg($background);
  19. $width=imagesx($back);
  20. $height=imagesy($back);
  21. $new=imagecreatetruecolor($width, $height);
  22. for($y=0; $y < $height; $y++){
  23. imagecopy($new, $back,0, $height-$y-1, 0, $y, $width, 1);
  24. }
  25. imagejpeg($new, $newfile);
  26. imagedestroy($back);
  27. imagedestroy($new);
  28. }
  29. turn_y("./images/hee.jpg", "./images/hee11.jpg");
  30. turn_x("./images/hee.jpg", "./images/hee12.jpg");
  31. ?>

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!