PHP gd ライブラリは画像サイズのトリミングとスケーリングを実装します

WBOY
リリース: 2016-07-25 08:51:47
オリジナル
2098 人が閲覧しました
  1. //画像のトリミングとスケーリング関数
  2. //$filepath画像パス、$percentスケーリングパーセンテージ
  3. function imagepress($filepath,$percent='0.5'){
  4. //画像タイプ
  5. header('Content- Type: image/jpeg');
  6. // 新しい画像サイズを取得します
  7. list($width, $height) = getimagesize($filepath);
  8. $new_width = $width * $percent;
  9. $new_height = $height * $パーセント;
  10. // リサンプリング
  11. $image_p = imagecreatetruecolor($new_width, $new_height);
  12. $image = imagecreatefromjpeg($filepath);
  13. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $ new_height, $width, $height);
  14. // Output
  15. return imagejpeg($image_p, null, 100);
  16. }
コードをコピー

元の画像: php gd库图片大小裁剪与缩放

レンダリング: PHP gd ライブラリは画像サイズのトリミングとスケーリングを実装します

例:

  1. //$filepath 画像パス, $new_width 新しい幅, $new_height 新しい高さ

  2. function imagepress($filepath, $new_width, $new_height)
  3. {
  4. $source_info = getimagesize( $filepath );
  5. $source_width = $source_info[0];
  6. $source_height = $source_info[1];
  7. $source_mime = $source_info['mime'];
  8. $source_ratio = $source_height / $source_width;
  9. $target_ratio = $new_height / $new_width;
  10. // ソース画像が高すぎます

  11. if ($source_ratio > $target_ratio)
  12. {
  13. $cropped_width = $source_width;
  14. $cropped_height = $source_width * $target_ratio ;
  15. $source_x = 0;
  16. $source_y = ($source_height - $cropped_height) / 2;
  17. }
  18. // ソース画像が広すぎます
  19. elseif ($source_ratio {
  20. $cropped_width = $source_height / $ target_ratio;
  21. $cropped_height = $source_height;
  22. $source_x = ($source_width - $cropped_width) / 2;
  23. $source_y = 0;
  24. }
  25. // ソース画像は中程度です
  26. else
  27. {
  28. $cropped_width = $ source_width;
  29. $ Cropped_height = $source_height;
  30. $source_x = 0;
  31. $source_y = 0;
  32. }
  33. switch ($source_mime)
  34. {
  35. case 'image/gif':
  36. $source_image = imagecreatefromgif($filepath);
  37. Break;
  38. case 'image/jpeg':
  39. $source_image = imagecreatefromjpeg($filepath);
  40. break;
  41. case 'image/png':
  42. $source_image = imagecreatefrompng($filepath);
  43. break;
  44. default:
  45. return false ;
  46. break;
  47. }
  48. $target_image = imagecreatetruecolor($new_width, $new_height);
  49. $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
  50. // トリミングされた
  51. imagecopy($cropped_image, $source_image, 0, 0, $ source_x, $source_y , $cropped_width, $cropped_height);
  52. // スケーリング
  53. imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $new_width, $new_height, $cropped_width, $cropped_height);
  54. header(' Content-Type: image/jpeg');
  55. imagejpeg($target_image);
  56. imagedestroy($source_image);
  57. imagedestroy($target_image);
  58. imagedestroy($cropped_image);
  59. }
コードをコピー


ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!