Home >Backend Development >PHP Tutorial >PHP thumbnail code, imagecopyresampled function generates thumbnails

PHP thumbnail code, imagecopyresampled function generates thumbnails

WBOY
WBOYOriginal
2016-07-25 08:51:29833browse
  1. function CreateImage($SrcImageUrl, $DirImageUrl, $Width, $Height)

  2. {
  3. $img;
  4. $srcw;
  5. $new_width;
  6. $srch;
  7. $new_height;
  8. // 图片类型
  9. $type = substr(strrchr($SrcImageUrl, "."), 1);
  10. // 初始化图像
  11. if($type == "jpg")
  12. $img = imagecreatefromjpeg($SrcImageUrl);
  13. if($type == "gif")
  14. $img = imagecreatefromgif($SrcImageUrl);
  15. if($type == "png")
  16. $img = imagecreatefrompng($SrcImageUrl);

  17. $srcw = imagesx($img);

  18. $srch = imagesy($img);

  19. if ($srcw / $srch > $Width / $Height)

  20. {
  21. if ($srcw > $Width)
  22. {
  23. $new_width = $Width;
  24. $new_height = $srch * ($Width / $srcw);
  25. }
  26. else
  27. {
  28. $new_width = $srcw;
  29. $new_height = $srch;
  30. }
  31. }
  32. else
  33. {
  34. if ($srch > $Height)
  35. {
  36. $new_height = $Height;
  37. $new_width = $srcw * ($Height / $srch);
  38. }
  39. else
  40. {
  41. $new_width = $srcw;
  42. $new_height = $srch;
  43. }
  44. }

  45. $new_image = imagecreatetruecolor($new_width, $new_height);

  46. imagecopyresampled($new_image, $img, 0, 0, 0, 0, $new_width, $new_height, $srcw, $srch);
  47. imagejpeg($new_image, $DirImageUrl);

  48. imagedestroy($img);

  49. imagedestroy($new_image);
  50. }

复制代码

3. Improved code:

  1. /*

  2. *$o_photo Original image path

  3. *$d_photo Processed image Path

  4. *$width Define width

  5. *$height Define height

  6. *Call method cutphoto("test.jpg ","temp.jpg",256,146);

  7. */

  8. function cutphoto($o_photo,$d_photo,$width,$height){< ;/p>

  9. $temp_img = imagecreatefromjpeg($o_photo);

  10. $o_width = imagesx($temp_img); //Get the original image width

  11. < ;p>$o_height = imagesy($temp_img); //Get the original image height

  12. //Judge the processing method

  13. if($width>$o_width || $height>$o_height){//The width or height of the original image is smaller than the specified size, and is compressed

  14. $newwidth=$o_width;

  15. $newheight=$o_height;

  16. if($o_width>$width){

  17. $newwidth=$width;

  18. }

  19. if($newheight>$height){

  20. < ;p>$newwidth=$newwidth*$height/$newheight;

  21. $newheight=$height;

  22. }

  23. < p>//Thumbnail image

  24. $new_img = imagecreatetruecolor($newwidth, $newheight);

  25. imagecopyresampled($new_img, $temp_img, 0, 0 , 0, 0, $newwidth, $newheight, $o_width, $o_height);

  26. imagejpeg($new_img , $d_photo);

  27. imagedestroy($ new_img);

  28. }else{//The original image width and height are larger than the specified size, compress and then crop

  29. if($o_height*$width /$o_width>$height){//First make sure the width is the same as the specification. If the height is larger than the specification, then ok

  30. $newwidth=$width;

  31. $newheight=$o_height*$width/$o_width;

  32. $x=0;

  33. $y=($newheight-$height)/2;

  34. }else{ // Otherwise, make sure the height is the same as specified, and the width is adaptive

  35. $newwidth=$o_width*$height/$o_height;

  36. $newheight=$height;

  37. $x=($newwidth-$width)/2;

  38. $y=0 ;

  39. }

  40. //Thumbnail image

  41. $new_img = imagecreatetruecolor($newwidth, $newheight);< ;/p>

  42. imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);

  43. imagejpeg( $new_img , $d_photo);

  44. imagedestroy($new_img);

  45. $temp_img = imagecreatefromjpeg($d_photo);

  46. < p>$o_width = imagesx($temp_img); //Get the thumbnail width

  47. $o_height = imagesy($temp_img); //Get the thumbnail height

  48. < p>//Crop the image

  49. $new_imgx = imagecreatetruecolor($width,$height);

  50. imagecopyresampled($new_imgx,$temp_img,0,0, $x,$y,$width,$height,$width,$height);

  51. imagejpeg($new_imgx , $d_photo);

  52. imagedestroy( $new_imgx);

  53. }
  54. }
  55. ?>

Copy code


Statement:
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