一江春水向東流

          做一個有思想的人,期待與每一位熱愛思考的人交流,您的關注是對我最大的支持。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            44 隨筆 :: 139 文章 :: 81 評論 :: 0 Trackbacks

          PHP令我們驚喜的就是在圖形圖象處理方面要憂于ASP,用GD庫PHP就可以輕松的實現縮略圖。這一篇文章我們的目的就是用GD來生成縮略圖,PHP可以把縮略圖直接生成輸送到瀏覽器也可以以文件的形式把其存儲到硬盤當中。

          ?

          在生成縮略圖的過程當中我們需要用到GD庫當中的幾個函數:

          getimagesize(string filename [,array var])),取得圖像的信息,返回值是一人array,包括幾項信息$var[0]----返回圖像的width,$var[1]----返回height,[2]返回圖像文件的type,[4]返回的是與<img src="">當中的wdith,height有關的width="",height=""信息。

          imageX(resource image)

          imageY(resource image)? 返回圖像的寬和高

          imagecopyresized(des img,src img,int des_x,int des_y,int src_x,int src_y,int des_w,int des_h,int src_w,int src_y)? 復制并截取區域圖像

          imagecreatetruecolor(int width,int height)? 創建一個真彩圖

          imagejpeg(resource image)

          下面就是Code:
          <?php
          # Constants
          define(IMAGE_BASE, '/var/www/html/mbailey/images');
          define(MAX_WIDTH, 150);
          define(MAX_HEIGHT, 150);

          # Get image location
          $image_file = str_replace('..', '', $_SERVER['QUERY_STRING']);
          $image_path = IMAGE_BASE . "/$image_file";

          # Load image
          $img = null;
          $ext = strtolower(end(explode('.', $image_path)));
          if ($ext == 'jpg' || $ext == 'jpeg') {
          ??? $img = @imagecreatefromjpeg($image_path);
          } else if ($ext == 'png') {
          ??? $img = @imagecreatefrompng($image_path);
          # Only if your version of GD includes GIF support
          } else if ($ext == 'gif') {
          ??? $img = @imagecreatefrompng($image_path);
          }

          # If an image was successfully loaded, test the image for size
          if ($img) {

          ??? # Get image size and scale ratio
          ??? $width = imagesx($img);
          ??? $height = imagesy($img);
          ??? $scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

          ??? # If the image is larger than the max shrink it
          ??? if ($scale < 1) {
          ??????? $new_width = floor($scale*$width);
          ??????? $new_height = floor($scale*$height);

          ??????? # Create a new temporary image
          ??????? $tmp_img = imagecreatetruecolor($new_width, $new_height);

          ??????? # Copy and resize old image into new image
          ??????? imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
          ???????????????????????? $new_width, $new_height, $width, $height);
          ??????? imagedestroy($img);
          ??????? $img = $tmp_img;
          ??? }
          }

          # Create error image if necessary
          if (!$img) {
          ??? $img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
          ??? imagecolorallocate($img,0,0,0);
          ??? $c = imagecolorallocate($img,70,70,70);
          ??? imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
          ??? imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
          }

          # Display the image
          header("Content-type: image/jpeg");
          imagejpeg($img);
          ?>

          我們把上面的Code存儲為test.php,然后通過test.php?image name的形式來訪問,結果會讓你驚喜的,因為在這里你看到了PHP的優點,它可以讓ASP相形見絀。

          上面的這段代碼當中我們通過end(explode(".",$image_path)來取得文件的擴展名,但是我感覺還是不理想。這樣是能夠取得文件的類型的,因為end()函數會跳到本array的最后一個單元,但是如果我們采用getimagesize()會取得更為強大的專門針對于圖像文件的類型。

          本程序顯示的縮略圖是限制寬高都在150內,然后用min()函數來取得它們比值的最小值來計算縮略圖的寬和高,并且通過一系列的GD庫函數來取得相應的信息,并且呈現給瀏覽器,當然你也可以寫到你所使用的硬盤當中。

          好了,這就是PHP的縮略圖功能,大家覺得有什么好的意見可以多多拍磚!

          posted on 2008-05-24 20:20 allic 閱讀(317) 評論(0)  編輯  收藏 所屬分類: PHP+MySQL開發
          主站蜘蛛池模板: 塔城市| 黑河市| 新平| 民权县| 太和县| 正宁县| 江永县| 保亭| 明光市| 驻马店市| 定安县| 连平县| 福建省| 灵武市| 清远市| 孙吴县| 丰台区| 三穗县| 泰兴市| 凤庆县| 富锦市| 深圳市| 新乐市| 如皋市| 庆城县| 温泉县| 盘锦市| 闽清县| 雷波县| 于田县| 太原市| 西乌珠穆沁旗| 丰镇市| 苏尼特左旗| 田林县| 永新县| 庆城县| 文登市| 丹阳市| 措美县| 封开县|