ホームページ  >  記事  >  バックエンド開発  >  PHP画像処理関数の完全なコレクション

PHP画像処理関数の完全なコレクション

不言
不言オリジナル
2018-06-06 09:18:195784ブラウズ

この記事では主に、特定の参考価値のある PHP 画像処理関数の完全なコレクションを紹介しますので、必要な友人に参考にしてください。

拡大縮小、切り抜き、拡大縮小、反転、回転、透明度、シャープ化などを含む、PHP 画像処理コード共有。
1. 画像リソースを作成します
imagecreatetruecolor(width,height);
imagecreatefromgif(画像名);
imagecreatefrompng(画像名);
imagecreatefromjpeg(画像名);画像リソース、保存パス);
imagepng()
imagejpeg();
2. 画像属性を取得します
imagesx(res//height
getimagesize (ファイルパス)
インデックス 0 には画像の幅のピクセル値が含まれ、インデックス 1 には画像の高さのピクセル値が含まれます: 1 = GIF、2 = JPG、3 = PNG、4 = SWF、5 = PSD、6 = BMP、7 = TIFF(インテル バイト オーダー)、8 = TIFF(モトローラ バイト オーダー)、9 = JPC、10 = JP2、11 = JPX、12 = JB2、13 = SWC、14 = WBMP、16 = XBM。これらのタグは、PHP 4.3.0 で追加された新しい IMAGETYPE 定数に対応します。インデックス 3 は、「height="yyy" width="」という内容のテキスト文字列であり、IMG に直接使用できます。タグ。
画像リソースを破棄する
imagedestroy(画像リソース);
3. 透明処理

PNGとjpegの透明色は正常ですが、GIFのみが異常です
imagecolortransparent(リソース画像 [,int color])//透明になる色を設定します
imagecolorstotal()
imagecolorforindex();
4. 画像のトリミング

imagecopyresize()
imagecopyresampled();
5 . ウォーターマーク (テキスト、画像) を追加します

文字列エンコード変換文字列 iconv ( string $in_charset , string $out_charset , string $str )
6. 画像回転
imagerotate(); // 指定した角度で​​画像を反転

7. 画像反転
Y 軸に沿って X 軸に沿って反転

8. シャープ化

imagecolorsforindex ()
imagecolorat()
画像にグラフィックを描画 $img=imagecreatefromgif("./images/map.gif");

  1. <?PHP
     /**
     * 图片锐化处理
     */
     $red= imagecolorallocate($img, 255, 0, 0);
     imageline($img, 0, 0, 100, 100, $red);
     imageellipse($img, 200, 100, 100, 100, $red);
     imagegif($img, "./images/map2.gif");
     imagedestroy($img);

写真の通常のスケーリング

  1. <?php
     $filename="./images/hee.jpg";
     $per=0.3;
     list($width, $height)=getimagesize($filename);
     $n_w=$width*$per;
     $n_h=$width*$per;
     $new=imagecreatetruecolor($n_w, $n_h);
     $img=imagecreatefromjpeg($filename);
     //拷贝部分图像并调整
     imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
     //图像输出新图片、另存为
     imagejpeg($new, "./images/hee2.jpg");
     imagedestroy($new);
     imagedestroy($img);

写真は同じ比率で拡大縮小され、透明色は拡大縮小されません加工済み

  1. <?php
     function thumn($background, $width, $height, $newfile) {
     list($s_w, $s_h)=getimagesize($background);//获取原图片高度、宽度
     if ($width && ($s_w < $s_h)) {
     $width = ($height / $s_h) * $s_w;
     } else {
     $height = ($width / $s_w) * $s_h;
     }
     $new=imagecreatetruecolor($width, $height);
     $img=imagecreatefromjpeg($background);
     imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
     imagejpeg($new, $newfile);
     imagedestroy($new);
     imagedestroy($img);
     }
     thumn("images/hee.jpg", 200, 200, "./images/hee3.jpg");

gif透過色加工

  1. <?php
     function thumn($background, $width, $height, $newfile) {
     list($s_w, $s_h)=getimagesize($background);
     if ($width && ($s_w < $s_h)) {
     $width = ($height / $s_h) * $s_w;
     } else {
     $height = ($width / $s_w) * $s_h;
     }
     $new=imagecreatetruecolor($width, $height);
     $img=imagecreatefromgif($background);
     $otsc=imagecolortransparent($img);
     if($otsc >=0 && $otst < imagecolorstotal($img)){//判断索引色
     $tran=imagecolorsforindex($img, $otsc);//索引颜色值
     $newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]);
     imagefill($new, 0, 0, $newt);
     imagecolortransparent($new, $newt);
     }
     imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
     imagegif($new, $newfile);
     imagedestroy($new);
     imagedestroy($img);
     }
     thumn("images/map.gif", 200, 200, "./images/map3.gif");

画像のトリミング

  1. <?php
    /**
    * 图片裁剪处理
    * edit by www.jbxue.com
    */
    function cut($background, $cut_x, $cut_y, $cut_width, $cut_height, $location){
    $back=imagecreatefromjpeg($background);
    $new=imagecreatetruecolor($cut_width, $cut_height);
    imagecopyresampled($new, $back, 0, 0, $cut_x, $cut_y, $cut_width, $cut_height,$cut_width,$cut_height);
    imagejpeg($new, $location);
    imagedestroy($new);
    imagedestroy($back);
    }
    cut("./images/hee.jpg", 440, 140, 117, 112, "./images/hee5.jpg");
    ?>

画像透かしテキスト透かし

图片旋转

  1. <?PHP
     /**
     * 图片旋转
     */
     $back=imagecreatefromjpeg("./images/hee.jpg");
     $new=imagerotate($back, 45, 0);
     imagejpeg($new, "./images/hee9.jpg");
     ?>

图片水平翻转垂直翻转

点击(此处)折叠或打开

  1. <?php
     /**
     * 图片水平翻转 垂直翻转
     */
     function turn_y($background, $newfile){
     $back=imagecreatefromjpeg($background);
     $width=imagesx($back);
     $height=imagesy($back);
     $new=imagecreatetruecolor($width, $height);
     for($x=0; $x < $width; $x++){
     imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
     }
     imagejpeg($new, $newfile);
     imagedestroy($back);
     imagedestroy($new);
     }
     function turn_x($background, $newfile){
     $back=imagecreatefromjpeg($background);
     $width=imagesx($back);
     $height=imagesy($back);
     $new=imagecreatetruecolor($width, $height);
     for($y=0; $y < $height; $y++){
     imagecopy($new, $back,0, $height-$y-1, 0, $y, $width, 1);
     }
     imagejpeg($new, $newfile);
     imagedestroy($back);
     imagedestroy($new);
     }
     turn_y("./images/hee.jpg", "./images/hee11.jpg");
     turn_x("./images/hee.jpg", "./images/hee12.jpg");
     ?>
    相关推荐:
  2. php 实用函数,php函数

  3. (转)PHP常用函数,php函数

以上がPHP画像処理関数の完全なコレクションの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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