高品質なサムネイルを生成するphp GDライブラリ(例)

WBOY
リリース: 2016-07-25 08:55:15
オリジナル
744 人が閲覧しました
  1. $FILENAME="image.thumb";

  2. // 画像のサイズを生成
  3. $RESIZEWIDTH=400;
  4. // 画像の高さを生成
  5. $RESIZEHEIGHT=400;

  6. function ResizeImage($im,$maxwidth,$maxheight,$name){

  7. $width = imagex($im);
  8. $height = imagey($im);
  9. if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
  10. if($maxwidth && $width > $maxwidth){
  11. $widthratio = $maxwidth/ $幅;
  12. $RESIZEWIDTH=true;
  13. }
  14. if($maxheight && $height > $maxheight){
  15. $heightratio = $maxheight/$height;
  16. $RESIZEHEIGHT=true;
  17. }
  18. if($RESIZEWIDTH && $RESIZEHEIGHT){
  19. if($widthratio < $heightratio){
  20. $ratio = $widthratio;
  21. }else{
  22. $ratio = $heightratio;
  23. }
  24. }elseif($RESIZEWIDTH){
  25. $ratio = $widthratio;
  26. }elseif($RESIZEHEIGHT){
  27. $ratio = $heightratio;
  28. }
  29. $newwidth = $width * $ratio;
  30. $newheight = $height * $ratio;
  31. if(function_exists("imagecopyresampled")){
  32. $newim = imagecreatetruecolor($newwidth, $newheight);
  33. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  34. }else{
  35. $newim = imagecreate($newwidth, $newheight);
  36. imagecopyresize($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  37. }
  38. ImageJpeg ($newim,$name . ".jpg");
  39. ImageDestroy ($newim);
  40. }else{
  41. ImageJpeg ($im,$name . ".jpg");
  42. }
  43. }

  44. if($_FILES['image']['size']){

  45. if($_FILES['image']['type'] == "画像/ pjpeg"){
  46. $im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
  47. }elseif($_FILES['image']['type'] == "image/x-png"){
  48. $im = imagecreatefrompng($_FILES['image']['tmp_name']);
  49. }elseif($_FILES['image']['type'] == "image/gif"){
  50. $im = imagecreatefromgif($_FILES['image']['tmp_name']);
  51. }
  52. if($im){
  53. if(file_exists("$FILENAME.jpg")){
  54. unlink("$FILENAME.jpg");
  55. }
  56. ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
  57. ImageDestroy ($im);
  58. }
  59. }
  60. ?>

复制代码

2、测试代码(demo.php)

  1. /**
  2. * PHP は高品質のサムネイルを生成します
  3. * by bbs.it-home.org
  4. */
  5. include('ResizeImage.php');
  6. if(!empty($_POST)){
  7. echo($FILENAME.".jpg?cache=".rand(0,999999));
  8. }
  9. ?>
复制代


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