当前位置:Gxlcms > PHP教程 > php图像处理函数例子汇总,php图片函数实例代码

php图像处理函数例子汇总,php图片函数实例代码

时间:2021-07-01 10:21:17 帮助过:19人阅读

  1. imagecolorsforindex()

  2. imagecolorat()
  3. 在图片上画图形 $img=imagecreatefromgif("./images/map.gif");

  4. /**

  5. * 图片锐化处理
  6. * edit by bbs.it-home.org
  7. */
  8. $red= imagecolorallocate($img, 255, 0, 0);

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

  10. imageellipse($img, 200, 100, 100, 100, $red);
  11. imagegif($img, "./images/map2.gif");
  12. imagedestroy($img);

图片普通缩放

  1. function thumn($background, $width, $height, $newfile) {
  2. list($s_w, $s_h)=getimagesize($background);//获取原图片高度、宽度
  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");

gif透明色处理

  1. /**

  2. * 图片裁剪处理
  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. ?>

图片加水印 文字水印

  1. /**

  2. *
  3. * 图片添加文字水印
  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", "细说PHP", 150, 250);
  13. //图片水印
  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);

图片旋转

  1. /**

  2. * 图片旋转
  3. * edit by bbs.it-home.org
  4. */
  5. $back=imagecreatefromjpeg("./images/hee.jpg");

  6. $new=imagerotate($back, 45, 0);

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

图片水平翻转垂直翻转

  1. /**

  2. * 图片水平翻转 垂直翻转
  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. ?>

人气教程排行