GDを使用してPHPで画像を操作する方法

墨辰丷
リリース: 2023-03-31 15:32:02
オリジナル
1579 人が閲覧しました

この記事では、PHP で GD を使用してアスペクト比を維持したサムネイルを作成する方法を主に紹介します。GD ライブラリを使用して画像を操作するスキルについて説明します。

この記事の例では、アスペクト比を維持するサムネイルを作成する PHP GD の方法の使用について説明します。詳細は次のとおりです。

/**
* Create a thumbnail image from $inputFileName no taller or wider than
* $maxSize. Returns the new image resource or false on error.
* Author: mthorn.net
*/
function thumbnail($inputFileName, $maxSize = 100)
{
 $info = getimagesize($inputFileName);
  $type = isset($info['type']) ? $info['type'] : $info[2];
  // Check support of file type
 if ( !(imagetypes() & $type) )
 {
   // Server does not support file type
   return false;
 }
  $width = isset($info['width']) ? $info['width'] : $info[0];
 $height = isset($info['height']) ? $info['height'] : $info[1];
  // Calculate aspect ratio
 $wRatio = $maxSize / $width;
 $hRatio = $maxSize / $height;
  // Using imagecreatefromstring will automatically detect the file type
 $sourceImage = imagecreatefromstring(file_get_contents($inputFileName));
  // Calculate a proportional width and height no larger than the max size.
 if ( ($width <= $maxSize) && ($height <= $maxSize) )
 {
   // Input is smaller than thumbnail, do nothing
   return $sourceImage;
 }
 elseif ( ($wRatio * $height) < $maxSize )
 {
   // Image is horizontal
   $tHeight = ceil($wRatio * $height);
   $tWidth = $maxSize;
 }
 else
 {
   // Image is vertical
   $tWidth = ceil($hRatio * $width);
   $tHeight = $maxSize;
 }
  $thumb = imagecreatetruecolor($tWidth, $tHeight);
  if ( $sourceImage === false )
 {
   // Could not load image
   return false;
 }
  // Copy resampled makes a smooth thumbnail
 imagecopyresampled($thumb,$sourceImage,0,0,0,0,$tWidth,$tHeight,$width,$height);
 imagedestroy($sourceImage);
  return $thumb;
}
 /**
* Save the image to a file. Type is determined from the extension.
* $quality is only used for jpegs.
* Author: mthorn.net
*/
function imageToFile($im, $fileName, $quality = 80)
{
 if ( !$im || file_exists($fileName) )
 {
   return false;
 }
  $ext = strtolower(substr($fileName, strrpos($fileName, &#39;.&#39;)));
  switch ( $ext )
 {
  case &#39;.gif&#39;:
  imagegif($im, $fileName);
  break;
  case &#39;.jpg&#39;:
  case &#39;.jpeg&#39;:
  imagejpeg($im, $fileName, $quality);
  break;
  case &#39;.png&#39;:
  imagepng($im, $fileName);
  break;
  case &#39;.bmp&#39;:
  imagewbmp($im, $fileName);
  break;
  default:
  return false;
 }
  return true;
}
$im = thumbnail(&#39;temp.jpg&#39;, 100);
imageToFile($im, &#39;temp-thumbnail.jpg&#39;);
ログイン後にコピー

概要: 上記がこの記事の全内容です。すべての人にとって役立つことを願っています。学習は役立ちます。

関連する推奨事項:

php を使用してデータベースの読み取り、判定、セッション ログインを行う方法

php ファイルのアップロードに関連するファイルとフォームの操作とデータベース操作

php のエラー処理に関する一般的なヒント

#

以上がGDを使用してPHPで画像を操作する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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