ホームページ  >  記事  >  バックエンド開発  >  変形なしのスクリーンショットの PHP コード

変形なしのスクリーンショットの PHP コード

WBOY
WBOYオリジナル
2016-07-25 08:46:10850ブラウズ
変形なし、PHP
  1. function my_image_resize($src_file, $dst_file , $new_width , $new_height)
  2. {
  3. $src_img=imagecreatefromjpeg($src_file);
  4. $w=imagesx($src_img);
  5. $h= Imagesy($src_img);
  6. $ratio_w=1.0 * $new_width / $w;
  7. $ratio_h=1.0 * $new_height / $h;
  8. $ratio=1.0;
  9. // 生成された画像の高さと幅は、元の比率、または両方が大きい場合、拡大には大きな比率を使用し、縮小には大きな比率を使用する (縮小された比率は小さくなります)
  10. if( ($ratio_w < 1 && $ratio_h < 1 ) || ($ratio_w > 1 && $ratio_h > 1))
  11. {
  12. if($ratio_w < $ratio_h)
  13. {
  14. $ratio = $ratio_h ; // ケース 1、幅の比率は高さ方向、高さの比率標準に従ってトリミングするか、ズームインします
  15. }else {
  16. $ratio = $ratio_w ;
  17. }
  18. // アスペクト比がターゲット要件をちょうど満たす中間一時画像を定義します
  19. $inter_w=(int)( $new_width / $ratio);
  20. $inter_h=(int) ($new_height / $ratio);
  21. $inter_img=imagecreatetruecolor($inter_w , $inter_h);
  22. imagecopy($inter_img, $src_img, 0,0,0, 0,$inter_w,$inter_h) ;
  23. // ターゲット画像のサイズとして最大辺の長さを持つ一時画像を生成します $ratio
  24. // 新しい画像を定義します
  25. $new_img=imagecreatetruecolor($new_width,$new_height);
  26. imagecopyresampled($new_img,$ inter_img,0,0,0,0,$new_width,$new_height,$inter_w,$inter_h);
  27. //imagejpeg($new_img, $dst_file,100) // 画像を保存します
  28. }
  29. // end if 1
  30. // 2 対象画像の片側が元の画像より大きく、片側が元の画像より小さい まず通常の画像を拡大してからトリミングします
  31. //if( ($ rate_w < 1 && $ratio_h > 1) || ($ratio_w > ;1 && $ratio_h <1) )
  32. $ratio=$ratio_h>$ratio_w?比率が大きい方の値
  33. //中央に大きな画像を定義し、その画像の高さまたは幅を対象画像と同じにし、元の画像を拡大します
  34. $inter_w=(int)($w * $ratio) ;
  35. $inter_h=(int) ($h * $ratio);
  36. $inter_img=imagecreatetruecolor ($inter_w , $inter_h);
  37. //元の画像をスケーリング後にトリミングします
  38. imagecopyresampled($inter_img,$src_img,0 ,0,0,0,$inter_w,$inter_h,$w,$h);
  39. // 新しい画像を定義します
  40. $new_img=imagecreatetruecolor($new_width,$new_height);
  41. imagecopy($new_img, $inter_img, 0 ,0,0,0,$new_width,$new_height);
  42. }
  43. imagejpeg($new_img , $dst_file,100); //ストレージ画像
  44. }
  45. $filename = "110.jpg";元の画像
  46. $fileto="120.jpg";//スクリーンショットを撮った後に保存するパス
  47. $new_w =300;//幅を設定
  48. $new_h=350; //高さを設定
  49. my_image_resize($filename,$; fileto,$new_w,$new_h);
  50. ?>
コードをコピー

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。