検索
ホームページバックエンド開発PHPチュートリアルPHPフル機能の無変形画像切り出し操作クラスと使用例

この記事の例では、PHP でのフル機能の非変形画像トリミング操作クラスと使用法について説明します。参考として皆さんと共有してください。詳細は次のとおりです。

写真をトリミングしたり、サムネイルを生成したりする必要がある場合、基本的には問題ありません。必要な機能はすべて内部にあり、すべて変形できません。

ここでは4つのモードに分けています:

1.任意の画像を指定したサイズにトリミングし、それを超える場合は再生中にトリミングし、ズームを最大化し、十分ではない場合は引き伸ばします、
2.それを超えると、再生中にトリミングされ、十分でない場合は、引き伸ばさずに再生中にトリミングされます。つまり、拡大やトリミングは行わず、縮小のみが行われます。これにより、パディングが生成されます。 PNG 透過で削除します
3. すべての画像情報を保持します。トリミングなし、スケーリングのみ、パディングが不十分です。
4. すべての画像情報を保存します。クロップなし、ズームのみ、必要に応じてパディングなし。生成される画像は実際の有効ピクセル サイズです。たとえば、画像が 600X600 で、120X100 を生成したい場合、スケーリング後の実際の有効ピクセルは 100X100 なので、 100X100 の画像が生成され、3 番目のモードです。このモードでは 120X100 のサイズが生成され、フィラーが含まれます

コードは次のとおりです

<?php
/**
* Author : smallchicken
* mode 1 : 强制裁剪,生成图片严格按照需要,不足放大,超过裁剪,图片始终铺满
* mode 2 : 和1类似,但不足的时候 不放大 会产生补白,可以用png消除。
* mode 3 : 只缩放,不裁剪,保留全部图片信息,会产生补白,
* mode 4 : 只缩放,不裁剪,保留全部图片信息,生成图片大小为最终缩放后的图片有效信息的实际大小,不产生补白
* 默认补白为白色,如果要使补白成透明像素,请使用SaveAlpha()方法代替SaveImage()方法
*
* 调用方法:
*
* $ic=new ImageCrop(&#39;old.jpg&#39;,&#39;afterCrop.jpg&#39;);
* $ic->Crop(120,80,2);
* $ic->SaveImage();
*    //$ic->SaveAlpha();将补白变成透明像素保存
* $ic->destory();
*
*
*/
class ImageCrop {
  var $sImage;
  var $dImage;
  var $src_file;
  var $dst_file;
  var $src_width;
  var $src_height;
  var $src_ext;
  var $src_type;
  function ImageCrop($src_file,$dst_file=&#39;&#39;) {
    $this->src_file=$src_file;
    $this->dst_file=$dst_file;
    $this->LoadImage();
  }
  function SetSrcFile($src_file) {
    $this->src_file=$src_file;
  }
  function SetDstFile($dst_file) {
    $this->dst_file=$dst_file;
  }
  function LoadImage() {
    list($this->src_width, $this->src_height, $this->src_type) = getimagesize($this->src_file);
    switch($this->src_type) {
      case IMAGETYPE_JPEG :
      $this->sImage=imagecreatefromjpeg($this->src_file);
      $this->ext=&#39;jpg&#39;;
      break;
      case IMAGETYPE_PNG :
      $this->sImage=imagecreatefrompng($this->src_file);
      $this->ext=&#39;png&#39;;
      break;
      case IMAGETYPE_GIF :
      $this->sImage=imagecreatefromgif($this->src_file);
      $this->ext=&#39;gif&#39;;
      break;
      default:
      exit();
    }
  }
  function SaveImage($fileName=&#39;&#39;) {
    $this->dst_file=$fileName ? $fileName : $this->dst_file;
    switch($this->src_type) {
      case IMAGETYPE_JPEG :
      imagejpeg($this->dImage,$this->dst_file,100);
      break;
      case IMAGETYPE_PNG :
      imagepng($this->dImage,$this->dst_file);
      break;
      case IMAGETYPE_GIF :
      imagegif($this->dImage,$this->dst_file);
      break;
      default:
      break;
    }
  }
  function OutImage() {
    switch($this->src_type) {
      case IMAGETYPE_JPEG :
      header(&#39;Content-type: image/jpeg&#39;);
      imagejpeg($this->dImage);
      break;
      case IMAGETYPE_PNG :
      header(&#39;Content-type: image/png&#39;);
      imagepng($this->dImage);
      break;
      case IMAGETYPE_GIF :
      header(&#39;Content-type: image/gif&#39;);
      imagegif($this->dImage);
      break;
      default:
      break;
    }
  }
  function SaveAlpha($fileName=&#39;&#39;) {
    $this->dst_file=$fileName ? $fileName . &#39;.png&#39; : $this->dst_file .&#39;.png&#39;;
    imagesavealpha($this->dImage, true);
    imagepng($this->dImage,$this->dst_file);
  }
  function OutAlpha() {
    imagesavealpha($this->dImage, true);
    header(&#39;Content-type: image/png&#39;);
    imagepng($this->dImage);
  }
  function destory() {
    imagedestroy($this->sImage);
    imagedestroy($this->dImage);
  }
  function Crop($dst_width,$dst_height,$mode=1,$dst_file=&#39;&#39;) {
    if($dst_file) $this->dst_file=$dst_file;
    $this->dImage = imagecreatetruecolor($dst_width,$dst_height);
    $bg = imagecolorallocatealpha($this->dImage,255,255,255,127);
    imagefill($this->dImage, 0, 0, $bg);
    imagecolortransparent($this->dImage,$bg);
    $ratio_w=1.0 * $dst_width / $this->src_width;
    $ratio_h=1.0 * $dst_height / $this->src_height;
    $ratio=1.0;
    switch($mode) {
      case 1:    // always crop
      if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) {
        $ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
        $tmp_w = (int)($dst_width / $ratio);
        $tmp_h = (int)($dst_height / $ratio);
        $tmp_img=imagecreatetruecolor($tmp_w , $tmp_h);
        $src_x = (int) (($this->src_width-$tmp_w)/2) ;
        $src_y = (int) (($this->src_height-$tmp_h)/2) ;
        imagecopy($tmp_img, $this->sImage, 0,0,$src_x,$src_y,$tmp_w,$tmp_h);
        imagecopyresampled($this->dImage,$tmp_img,0,0,0,0,$dst_width,$dst_height,$tmp_w,$tmp_h);
        imagedestroy($tmp_img);
      } else {
        $ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
        $tmp_w = (int)($this->src_width * $ratio);
        $tmp_h = (int)($this->src_height * $ratio);
        $tmp_img=imagecreatetruecolor($tmp_w ,$tmp_h);
        imagecopyresampled($tmp_img,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
        $src_x = (int)($tmp_w - $dst_width) / 2 ;
        $src_y = (int)($tmp_h - $dst_height) / 2 ;
        imagecopy($this->dImage, $tmp_img, 0,0,$src_x,$src_y,$dst_width,$dst_height);
        imagedestroy($tmp_img);
      }
      break;
      case 2:    // only small
      if($ratio_w < 1 && $ratio_h < 1) {
        $ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
        $tmp_w = (int)($dst_width / $ratio);
        $tmp_h = (int)($dst_height / $ratio);
        $tmp_img=imagecreatetruecolor($tmp_w , $tmp_h);
        $src_x = (int) ($this->src_width-$tmp_w)/2 ;
        $src_y = (int) ($this->src_height-$tmp_h)/2 ;
        imagecopy($tmp_img, $this->sImage, 0,0,$src_x,$src_y,$tmp_w,$tmp_h);
        imagecopyresampled($this->dImage,$tmp_img,0,0,0,0,$dst_width,$dst_height,$tmp_w,$tmp_h);
        imagedestroy($tmp_img);
      } elseif($ratio_w > 1 && $ratio_h > 1) {
        $dst_x = (int) abs($dst_width - $this->src_width) / 2 ;
        $dst_y = (int) abs($dst_height -$this->src_height) / 2;
        imagecopy($this->dImage, $this->sImage,$dst_x,$dst_y,0,0,$this->src_width,$this->src_height);
      } else {
        $src_x=0;
        $dst_x=0;
        $src_y=0;
        $dst_y=0;
        if(($dst_width - $this->src_width) < 0) {
          $src_x = (int) ($this->src_width - $dst_width)/2;
          $dst_x =0;
        } else {
          $src_x =0;
          $dst_x = (int) ($dst_width - $this->src_width)/2;
        }
        if( ($dst_height -$this->src_height) < 0) {
          $src_y = (int) ($this->src_height - $dst_height)/2;
          $dst_y = 0;
        } else {
          $src_y = 0;
          $dst_y = (int) ($dst_height - $this->src_height)/2;
        }
        imagecopy($this->dImage, $this->sImage,$dst_x,$dst_y,$src_x,$src_y,$this->src_width,$this->src_height);
      }
      break;
      case 3:    // keep all image size and create need size
      if($ratio_w > 1 && $ratio_h > 1) {
        $dst_x = (int)(abs($dst_width - $this->src_width )/2) ;
        $dst_y = (int)(abs($dst_height- $this->src_height)/2) ;
        imagecopy($this->dImage, $this->sImage, $dst_x,$dst_y,0,0,$this->src_width,$this->src_height);
      } else {
        $ratio = $ratio_w > $ratio_h ? $ratio_h : $ratio_w;
        $tmp_w = (int)($this->src_width * $ratio);
        $tmp_h = (int)($this->src_height * $ratio);
        $tmp_img=imagecreatetruecolor($tmp_w ,$tmp_h);
        imagecopyresampled($tmp_img,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
        $dst_x = (int)(abs($tmp_w -$dst_width )/2) ;
        $dst_y = (int)(abs($tmp_h -$dst_height)/2) ;
        imagecopy($this->dImage, $tmp_img, $dst_x,$dst_y,0,0,$tmp_w,$tmp_h);
        imagedestroy($tmp_img);
      }
      break;
      case 4:    // keep all image but create actually size
      if($ratio_w > 1 && $ratio_h > 1) {
        $this->dImage = imagecreatetruecolor($this->src_width,$this->src_height);
        imagecopy($this->dImage, $this->sImage,0,0,0,0,$this->src_width,$this->src_height);
      } else {
        $ratio = $ratio_w > $ratio_h ? $ratio_h : $ratio_w;
        $tmp_w = (int)($this->src_width * $ratio);
        $tmp_h = (int)($this->src_height * $ratio);
        $this->dImage = imagecreatetruecolor($tmp_w ,$tmp_h);
        imagecopyresampled($this->dImage,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
      }
      break;
    }
  }
  // end Crop
}
?>

この記事が PHP プログラミングのすべての人に役立つことを願っています。

PHP のフル機能の非変形画像トリミング操作クラスと使用例に関連するその他の記事については、PHP 中国語 Web サイトに注目してください。

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

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHPは、シンプルな構文と高い実行効率を備えたWeb開発に適しています。 2。Pythonは、簡潔な構文とリッチライブラリを備えたデータサイエンスと機械学習に適しています。

PHP:それは死にかけていますか、それとも単に適応していますか?PHP:それは死にかけていますか、それとも単に適応していますか?Apr 11, 2025 am 12:13 AM

PHPは死にかけていませんが、常に適応して進化しています。 1)PHPは、1994年以来、新しいテクノロジーの傾向に適応するために複数のバージョンの反復を受けています。 2)現在、電子商取引、コンテンツ管理システム、その他の分野で広く使用されています。 3)PHP8は、パフォーマンスと近代化を改善するために、JITコンパイラおよびその他の機能を導入します。 4)Opcacheを使用してPSR-12標準に従って、パフォーマンスとコードの品質を最適化します。

PHPの未来:適応と革新PHPの未来:適応と革新Apr 11, 2025 am 12:01 AM

PHPの将来は、新しいテクノロジーの傾向に適応し、革新的な機能を導入することで達成されます。1)クラウドコンピューティング、コンテナ化、マイクロサービスアーキテクチャに適応し、DockerとKubernetesをサポートします。 2)パフォーマンスとデータ処理の効率を改善するために、JITコンパイラと列挙タイプを導入します。 3)パフォーマンスを継続的に最適化し、ベストプラクティスを促進します。

PHPの抽象クラスまたはインターフェイスに対して、いつ特性を使用しますか?PHPの抽象クラスまたはインターフェイスに対して、いつ特性を使用しますか?Apr 10, 2025 am 09:39 AM

PHPでは、特性は方法が必要な状況に適していますが、継承には適していません。 1)特性により、クラスの多重化方法が複数の継承の複雑さを回避できます。 2)特性を使用する場合、メソッドの競合に注意を払う必要があります。メソッドの競合は、代替およびキーワードとして解決できます。 3)パフォーマンスを最適化し、コードメンテナビリティを改善するために、特性の過剰使用を避け、その単一の責任を維持する必要があります。

依存関係噴射コンテナ(DIC)とは何ですか?また、なぜPHPで使用するのですか?依存関係噴射コンテナ(DIC)とは何ですか?また、なぜPHPで使用するのですか?Apr 10, 2025 am 09:38 AM

依存関係噴射コンテナ(DIC)は、PHPプロジェクトで使用するオブジェクト依存関係を管理および提供するツールです。 DICの主な利点には、次のものが含まれます。1。デカップリング、コンポーネントの独立したもの、およびコードの保守とテストが簡単です。 2。柔軟性、依存関係を交換または変更しやすい。 3.テスト可能性、単体テストのために模擬オブジェクトを注入するのに便利です。

通常のPHPアレイと比較して、SPL SPLFIXEDARRAYとそのパフォーマンス特性を説明してください。通常のPHPアレイと比較して、SPL SPLFIXEDARRAYとそのパフォーマンス特性を説明してください。Apr 10, 2025 am 09:37 AM

SplfixedArrayは、PHPの固定サイズの配列であり、高性能と低いメモリの使用が必要なシナリオに適しています。 1)動的調整によって引き起こされるオーバーヘッドを回避するために、作成時にサイズを指定する必要があります。 2)C言語アレイに基づいて、メモリと高速アクセス速度を直接動作させます。 3)大規模なデータ処理とメモリに敏感な環境に適していますが、サイズが固定されているため、注意して使用する必要があります。

PHPは、ファイルを安全に処理する方法をどのように処理しますか?PHPは、ファイルを安全に処理する方法をどのように処理しますか?Apr 10, 2025 am 09:37 AM

PHPは、$ \ _ファイル変数を介してファイルのアップロードを処理します。セキュリティを確保するための方法には次のものが含まれます。1。アップロードエラー、2。ファイルの種類とサイズを確認する、3。ファイル上書きを防ぐ、4。ファイルを永続的なストレージの場所に移動します。

Null Coulescingオペレーター(??)およびNull Coulescing Assignment Operator(?? =)とは何ですか?Null Coulescingオペレーター(??)およびNull Coulescing Assignment Operator(?? =)とは何ですか?Apr 10, 2025 am 09:33 AM

JavaScriptでは、nullcoalescingoperator(??)およびnullcoalescingsignmentoperator(?? =)を使用できます。 1.??最初の非潜水金または非未定されたオペランドを返します。 2.??これらの演算子は、コードロジックを簡素化し、読みやすさとパフォーマンスを向上させます。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール