php將圖片保存為不同尺寸圖片的圖片類實例_PHP教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:Linux系統下php獲得系統分區信息的方法
      這篇文章主要介紹了Linux系統下php獲得系統分區信息的方法,涉及Linux下php系統分析的操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下 本文實例講述了Linux系統下php獲得系統分區信息的方法。分享給大家供大家參考。具體實現方法如下: ? 希望本文所述對大家的php

      這篇文章主要介紹了php將圖片保存為不同尺寸圖片的圖片類,涉及php圖片操作的保存、復制、縮略圖等常用技巧,并封裝成一個類文件以便于調用,非常具有實用價值,需要的朋友可以參考下

      本文實例講述了php將圖片保存為不同規格的圖片類。分享給大家供大家參考。具體如下:

      圖片處理類.imagecls.php如下:

      1. <?php 
      2. /** 
      3.   圖片處理類 
      4.  */ 
      5. class imagecls 
      6.   /** 
      7.    * 文件信息 
      8.    */ 
      9.   var $file = array(); 
      10.   /** 
      11.    * 保存目錄 
      12.    */ 
      13.   var $dir = ''
      14.   /** 
      15.    * 錯誤代碼 
      16.    */ 
      17.   var $error_code = 0; 
      18.   /** 
      19.    * 文件上傳最大KB 
      20.    */ 
      21.   var $max_size = -1; 
      22.   function es_imagecls() 
      23.   { 
      24.   } 
      25.   private function checkSize($size
      26.   { 
      27.     return !($size > $this->max_size) || (-1 == $this->max_size); 
      28.   } 
      29.   /** 
      30.    * 處理上傳文件 
      31.    * @param array $file 上傳的文件 
      32.    * @param string $dir 保存的目錄 
      33.    * @return bool 
      34.    */ 
      35.   function init($file$dir = 'temp'
      36.   { 
      37.     if(!is_array($file) || emptyempty($file) || !$this->isUploadFile($file['tmp_name']) || trim($file['name']) == '' || $file['size'] == 0) 
      38.     { 
      39.       $this->file = array(); 
      40.       $this->error_code = -1; 
      41.       return false; 
      42.     } 
      43.     else 
      44.     { 
      45.       $file['size'] = intval($file['size']); 
      46.       $file['name'] = trim($file['name']); 
      47.       $file['thumb'] = ''
      48.       $file['ext'] = $this->fileExt($file['name']); 
      49.       $file['name'] = htmlspecialchars($file['name'], ENT_QUOTES); 
      50.       $file['is_image'] = $this->isImageExt($file['ext']); 
      51.       $file['file_dir'] = $this->getTargetDir($dir); 
      52.       $file['prefix'] = md5(microtime(true)).rand(10,99); 
      53.       $file['target'] = "./public/".$file['file_dir'].'/'.$file['prefix'].'.jpg'//相對 
      54.       $file['local_target'] = APP_ROOT_PATH."public/".$file['file_dir'].'/'.$file['prefix'].'.jpg'//物理 
      55.       $this->file = &$file
      56.       $this->error_code = 0; 
      57.       return true; 
      58.     } 
      59.   } 
      60.   /** 
      61.    * 保存文件 
      62.    * @return bool 
      63.    */ 
      64.   function save() 
      65.   { 
      66.     if(emptyempty($this->file) || emptyempty($this->file['tmp_name'])) 
      67.       $this->error_code = -101; 
      68.     elseif(!$this->checkSize($this->file['size'])) 
      69.       $this->error_code = -105; 
      70.     elseif(!$this->file['is_image']) 
      71.       $this->error_code = -102; 
      72.     elseif(!$this->saveFile($this->file['tmp_name'], $this->file['local_target'])) 
      73.       $this->error_code = -103; 
      74.     elseif($this->file['is_image'] && (!$this->file['image_info'] = $this->getImageInfo($this->file['local_target'], true))) 
      75.     { 
      76.       $this->error_code = -104; 
      77.       @unlink($this->file['local_target']); 
      78.     } 
      79.     else 
      80.     { 
      81.       $this->error_code = 0; 
      82.       return true; 
      83.     } 
      84.     return false; 
      85.   } 
      86.   /** 
      87.    * 獲取錯誤代碼 
      88.    * @return number 
      89.    */ 
      90.   function error() 
      91.   { 
      92.     return $this->error_code; 
      93.   } 
      94.   /** 
      95.    * 獲取文件擴展名 
      96.    * @return string 
      97.    */ 
      98.   function fileExt($file_name
      99.   { 
      100.     return addslashes(strtolower(substr(strrchr($file_name'.'), 1, 10))); 
      101.   } 
      102.   /** 
      103.    * 根據擴展名判斷文件是否為圖像 
      104.    * @param string $ext 擴展名 
      105.    * @return bool 
      106.    */ 
      107.   function isImageExt($ext
      108.   { 
      109.     static $img_ext = array('jpg''jpeg''png''bmp','gif','giff'); 
      110.     return in_array($ext$img_ext) ? 1 : 0; 
      111.   } 
      112.   /** 
      113.    * 獲取圖像信息 
      114.    * @param string $target 文件路徑 
      115.    * @return mixed 
      116.    */ 
      117.   function getImageInfo($target
      118.   { 
      119.     $ext = es_imagecls::fileExt($target); 
      120.     $is_image = es_imagecls::isImageExt($ext); 
      121.     if(!$is_image
      122.       return false; 
      123.     elseif(!is_readable($target)) 
      124.       return false; 
      125.     elseif($image_info = @getimagesize($target)) 
      126.     { 
      127.       list($width$height$type) = !emptyempty($image_info) ? $image_info : array(''''''); 
      128.       $size = $width * $height
      129.       if($is_image && !in_array($typearray(1,2,3,6,13))) 
      130.         return false; 
      131.       $image_info['type'] = strtolower(substr(image_type_to_extension($image_info[2]),1)); 
      132.       return $image_info
      133.     } 
      134.     else 
      135.       return false; 
      136.   } 
      137.   /** 
      138.    * 獲取是否充許上傳文件 
      139.    * @param string $source 文件路徑 
      140.    * @return bool 
      141.    */ 
      142.   function isUploadFile($source
      143.   { 
      144.     return $source && ($source != 'none') && (is_uploaded_file($source) || is_uploaded_file(str_replace('\\\\', '\\', $source))); 
      145.   } 
      146.   /** 
      147.    * 獲取保存的路徑 
      148.    * @param string $dir 指定的保存目錄 
      149.    * @return string 
      150.    */ 
      151.   function getTargetDir($dir
      152.   {     
      153.     if (!is_dir(APP_ROOT_PATH."public/".$dir)) { 
      154.        @mkdir(APP_ROOT_PATH."public/".$dir); 
      155.        @chmod(APP_ROOT_PATH."public/".$dir, 0777); 
      156.     } 
      157.     return $dir
      158.   } 
      159.   /** 
      160.    * 保存文件 
      161.    * @param string $source 源文件路徑 
      162.    * @param string $target 目錄文件路徑 
      163.    * @return bool 
      164.    */ 
      165.   private function saveFile($source$target
      166.   { 
      167.     if(!es_imagecls::isUploadFile($source)) 
      168.       $succeed = false; 
      169.     elseif(@copy($source$target)) 
      170.       $succeed = true; 
      171.     elseif(function_exists('move_uploaded_file') && @move_uploaded_file($source$target)) 
      172.       $succeed = true; 
      173.     elseif (@is_readable($source) && (@$fp_s = fopen($source'rb')) && (@$fp_t = fopen($target'wb'))) 
      174.     { 
      175.       while (!feof($fp_s)) 
      176.       { 
      177.         $s = @fread($fp_s, 1024 * 512); 
      178.         @fwrite($fp_t$s); 
      179.       } 
      180.       fclose($fp_s); 
      181.       fclose($fp_t); 
      182.       $succeed = true; 
      183.     } 
      184.     if($succeed
      185.     { 
      186.       $this->error_code = 0; 
      187.       @chmod($target, 0644); 
      188.       @unlink($source); 
      189.     } 
      190.     else 
      191.     { 
      192.       $this->error_code = 0; 
      193.     } 
      194.     return $succeed
      195.   } 
      196.   public function thumb($image,$maxWidth=200,$maxHeight=50,$gen = 0,$interlace=true,$filepath = '',$is_preview = true) 
      197.   { 
      198.     $info = es_imagecls::getImageInfo($image); 
      199.     if($info !== false) 
      200.     { 
      201.       $srcWidth = $info[0]; 
      202.       $srcHeight = $info[1]; 
      203.       $type = $info['type']; 
      204.       $interlace = $interlace? 1:0; 
      205.       unset($info); 
      206.       if($maxWidth > 0 && $maxHeight > 0) 
      207.         $scale = min($maxWidth/$srcWidth$maxHeight/$srcHeight); // 計算縮放比例 
      208.       elseif($maxWidth == 0) 
      209.         $scale = $maxHeight/$srcHeight
      210.       elseif($maxHeight == 0) 
      211.         $scale = $maxWidth/$srcWidth
      212.       $paths = pathinfo($image); 
      213.       $paths['filename'] = trim(strtolower($paths['basename']),".".strtolower($paths['extension'])); 
      214.       $basefilename = explode("_",$paths['filename']); 
      215.       $basefilename = $basefilename[0]; 
      216.       if(emptyempty($filepath)) 
      217.       { 
      218.         if($is_preview
      219.         $thumbname = $paths['dirname'].'/'.$basefilename.'_'.$maxWidth.'x'.$maxHeight.'.jpg'
      220.         else 
      221.         $thumbname = $paths['dirname'].'/'.$basefilename.'o_'.$maxWidth.'x'.$maxHeight.'.jpg'
      222.       } 
      223.       else 
      224.         $thumbname = $filepath
      225.       $thumburl = str_replace(APP_ROOT_PATH,'./',$thumbname); 
      226.       if($scale >= 1) 
      227.       { 
      228.         // 超過原圖大小不再縮略 
      229.         $width  = $srcWidth
      230.         $height = $srcHeight;     
      231.         if(!$is_preview
      232.         {    
      233.           //非預覽模式寫入原圖 
      234.           file_put_contents($thumbname,file_get_contents($image));  //用原圖寫入       
      235.           return array('url'=>$thumburl,'path'=>$thumbname); 
      236.         } 
      237.       } 
      238.       else 
      239.       { 
      240.         // 縮略圖尺寸 
      241.         $width = (int)($srcWidth*$scale); 
      242.         $height = (int)($srcHeight*$scale); 
      243.       }    
      244.       if($gen == 1) 
      245.       { 
      246.         $width = $maxWidth
      247.         $height = $maxHeight
      248.       } 
      249.       // 載入原圖 
      250.       $createFun = 'imagecreatefrom'.($type=='jpg'?'jpeg':$type); 
      251.       if(!function_exists($createFun)) 
      252.         $createFun = 'imagecreatefromjpeg'
      253.       $srcImg = $createFun($image); 
      254.       //創建縮略圖 
      255.       if($type!='gif' && function_exists('imagecreatetruecolor')) 
      256.         $thumbImg = imagecreatetruecolor($width$height); 
      257.       else 
      258.         $thumbImg = imagecreate($width$height); 
      259.       $x = 0; 
      260.       $y = 0; 
      261.       if($gen == 1 && $maxWidth > 0 && $maxHeight > 0) 
      262.       { 
      263.         $resize_ratio = $maxWidth/$maxHeight
      264.         $src_ratio = $srcWidth/$srcHeight
      265.         if($src_ratio >= $resize_ratio
      266.         { 
      267.           $x = ($srcWidth - ($resize_ratio * $srcHeight)) / 2; 
      268.           $width = ($height * $srcWidth) / $srcHeight
      269.         } 
      270.         else 
      271.         { 
      272.           $y = ($srcHeight - ( (1 / $resize_ratio) * $srcWidth)) / 2; 
      273.           $height = ($width * $srcHeight) / $srcWidth
      274.         } 
      275.       } 
      276.       // 復制圖片 
      277.       if(function_exists("imagecopyresampled")) 
      278.         imagecopyresampled($thumbImg$srcImg, 0, 0, $x$y$width$height$srcWidth,$srcHeight); 
      279.       else 
      280.         imagecopyresized($thumbImg$srcImg, 0, 0, $x$y$width$height$srcWidth,$srcHeight); 
      281.       if('gif'==$type || 'png'==$type) { 
      282.         $background_color = imagecolorallocate($thumbImg, 0,255,0); // 指派一個綠色 
      283.         imagecolortransparent($thumbImg,$background_color); // 設置為透明色,若注釋掉該行則輸出綠色的圖 
      284.       } 
      285.       // 對jpeg圖形設置隔行掃描 
      286.       if('jpg'==$type || 'jpeg'==$type
      287.         imageinterlace($thumbImg,$interlace); 
      288.       // 生成圖片 
      289.       imagejpeg($thumbImg,$thumbname,100); 
      290.       imagedestroy($thumbImg); 
      291.       imagedestroy($srcImg); 
      292.       return array('url'=>$thumburl,'path'=>$thumbname); 
      293.      } 
      294.      return false; 
      295.   } 
      296.   public function make_thumb($srcImg,$srcWidth,$srcHeight,$type,$maxWidth=200,$maxHeight=50,$gen = 0) 
      297.   { 
      298.       $interlace = $interlace? 1:0; 
      299.       if($maxWidth > 0 && $maxHeight > 0) 
      300.         $scale = min($maxWidth/$srcWidth$maxHeight/$srcHeight); // 計算縮放比例 
      301.       elseif($maxWidth == 0) 
      302.         $scale = $maxHeight/$srcHeight
      303.       elseif($maxHeight == 0) 
      304.         $scale = $maxWidth/$srcWidth
      305.       if($scale >= 1) 
      306.       { 
      307.         // 超過原圖大小不再縮略 
      308.         $width  = $srcWidth
      309.         $height = $srcHeight
      310.       } 
      311.       else 
      312.       { 
      313.         // 縮略圖尺寸 
      314.         $width = (int)($srcWidth*$scale); 
      315.         $height = (int)($srcHeight*$scale); 
      316.       } 
      317.       if($gen == 1) 
      318.       { 
      319.         $width = $maxWidth
      320.         $height = $maxHeight
      321.       } 
      322.       //創建縮略圖 
      323.       if($type!='gif' && function_exists('imagecreatetruecolor')) 
      324.         $thumbImg = imagecreatetruecolor($width$height); 
      325.       else 
      326.         $thumbImg = imagecreatetruecolor($width$height); 
      327.       $x = 0; 
      328.       $y = 0; 
      329.       if($gen == 1 && $maxWidth > 0 && $maxHeight > 0) 
      330.       { 
      331.         $resize_ratio = $maxWidth/$maxHeight
      332.         $src_ratio = $srcWidth/$srcHeight
      333.         if($src_ratio >= $resize_ratio
      334.         { 
      335.           $x = ($srcWidth - ($resize_ratio * $srcHeight)) / 2; 
      336.           $width = ($height * $srcWidth) / $srcHeight
      337.         } 
      338.         else 
      339.         { 
      340.           $y = ($srcHeight - ( (1 / $resize_ratio) * $srcWidth)) / 2; 
      341.           $height = ($width * $srcHeight) / $srcWidth
      342.         } 
      343.       } 
      344.       // 復制圖片 
      345.       if(function_exists("imagecopyresampled")) 
      346.         imagecopyresampled($thumbImg$srcImg, 0, 0, $x$y$width$height$srcWidth,$srcHeight); 
      347.       else 
      348.         imagecopyresized($thumbImg$srcImg, 0, 0, $x$y$width$height$srcWidth,$srcHeight); 
      349.       if('gif'==$type || 'png'==$type) { 
      350.         $background_color = imagecolorallocate($thumbImg, 255,255,255); // 指派一個綠色 
      351.         imagecolortransparent($thumbImg,$background_color); // 設置為透明色,若注釋掉該行則輸出綠色的圖 
      352.       } 
      353.       // 對jpeg圖形設置隔行掃描 
      354.       if('jpg'==$type || 'jpeg'==$type
      355.         imageinterlace($thumbImg,$interlace); 
      356.       return $thumbImg
      357.   } 
      358.   public function water($source,$water,$alpha=80,$position="0"
      359.   { 
      360.     //檢查文件是否存在 
      361.     if(!file_exists($source)||!file_exists($water)) 
      362.       return false; 
      363.     //圖片信息 
      364.     $sInfo = es_imagecls::getImageInfo($source); 
      365.     $wInfo = es_imagecls::getImageInfo($water); 
      366.     //如果圖片小于水印圖片,不生成圖片 
      367.     if($sInfo["0"] < $wInfo["0"] || $sInfo['1'] < $wInfo['1']) 
      368.       return false; 
      369.     if(is_animated_gif($source)) 
      370.     { 
      371.       require_once APP_ROOT_PATH."system/utils/gif_encoder.php"
      372.       require_once APP_ROOT_PATH."system/utils/gif_reader.php"
      373.       $gif = new GIFReader(); 
      374.       $gif->load($source); 
      375.       foreach($gif->IMGS['frames'as $k=>$img
      376.       { 
      377.         $im = imagecreatefromstring($gif->getgif($k));    
      378.         //為im加水印 
      379.         $sImage=$im;     
      380.         $wCreateFun="imagecreatefrom".$wInfo['type']; 
      381.         if(!function_exists($wCreateFun)) 
      382.           $wCreateFun = 'imagecreatefromjpeg'
      383.         $wImage=$wCreateFun($water); 
      384.         //設定圖像的混色模式 
      385.         imagealphablending($wImage, true);    
      386.         switch (intval($position)) 
      387.         { 
      388.           case 0: break
      389.           //左上 
      390.           case 1: 
      391.             $posY=0; 
      392.             $posX=0; 
      393.             //生成混合圖像 
      394.             imagecopymerge($sImage$wImage$posX$posY, 0, 0, $wInfo[0],$wInfo[1],$alpha); 
      395.             break
      396.           //右上 
      397.           case 2: 
      398.             $posY=0; 
      399.             $posX=$sInfo[0]-$wInfo[0]; 
      400.             //生成混合圖像 
      401.             imagecopymerge($sImage$wImage$posX$posY, 0, 0, $wInfo[0],$wInfo[1],$alpha); 
      402.             break
      403.           //左下 
      404.           case 3: 
      405.             $posY=$sInfo[1]-$wInfo[1]; 
      406.             $posX=0; 
      407.             //生成混合圖像 
      408.             imagecopymerge($sImage$wImage$posX$posY, 0, 0, $wInfo[0],$wInfo[1],$alpha); 
      409.             break
      410.           //右下 
      411.           case 4: 
      412.             $posY=$sInfo[1]-$wInfo[1]; 
      413.             $posX=$sInfo[0]-$wInfo[0]; 
      414.             //生成混合圖像 
      415.             imagecopymerge($sImage$wImage$posX$posY, 0, 0, $wInfo[0],$wInfo[1],$alpha); 
      416.             break
      417.           //居中 
      418.           case 5: 
      419.             $posY=$sInfo[1]/2-$wInfo[1]/2; 
      420.             $posX=$sInfo[0]/2-$wInfo[0]/2; 
      421.             //生成混合圖像 
      422.             imagecopymerge($sImage$wImage$posX$posY, 0, 0, $wInfo[0],$wInfo[1],$alpha); 
      423.             break
      424.         } 
      425.         //end im加水印 
      426.         ob_start(); 
      427.         imagegif($sImage); 
      428.         $content = ob_get_contents(); 
      429.         ob_end_clean(); 
      430.         $frames [ ] = $content
      431.         $framed [ ] = $img['frameDelay']; 
      432.       } 
      433.       $gif_maker = new GIFEncoder ( 
      434.           $frames
      435.           $framed
      436.           0, 
      437.           2, 
      438.           0, 0, 0, 
      439.           "bin"  //bin為二進制  url為地址 
      440.        ); 
      441.       $image_rs = $gif_maker->GetAnimation ( ); 
      442.       //如果沒有給出保存文件名,默認為原圖像名 
      443.       @unlink($source); 
      444.       //保存圖像 
      445.       file_put_contents($source,$image_rs); 
      446.       return true; 
      447.     }  
      448.     //建立圖像 
      449.     $sCreateFun="imagecreatefrom".$sInfo['type']; 
      450.     if(!function_exists($sCreateFun)) 
      451.       $sCreateFun = 'imagecreatefromjpeg'
      452.     $sImage=$sCreateFun($source); 
      453.     $wCreateFun="imagecreatefrom".$wInfo['type']; 
      454.     if(!function_exists($wCreateFun)) 
      455.       $wCreateFun = 'imagecreatefromjpeg'
      456.     $wImage=$wCreateFun($water); 
      457.     //設定圖像的混色模式 
      458.     imagealphablending($wImage, true); 
      459.     switch (intval($position)) 
      460.     { 
      461.       case 0: break
      462.       //左上 
      463.       case 1: 
      464.         $posY=0; 
      465.         $posX=0; 
      466.         //生成混合圖像 
      467.         imagecopymerge($sImage$wImage$posX$posY, 0, 0, $wInfo[0],$wInfo[1],$alpha); 
      468.         break
      469.       //右上 
      470.       case 2: 
      471.         $posY=0; 
      472.         $posX=$sInfo[0]-$wInfo[0]; 
      473.         //生成混合圖像 
      474.         imagecopymerge($sImage$wImage$posX$posY, 0, 0, $wInfo[0],$wInfo[1],$alpha); 
      475.         break
      476.       //左下 
      477.       case 3: 
      478.         $posY=$sInfo[1]-$wInfo[1]; 
      479.         $posX=0; 
      480.         //生成混合圖像 
      481.         imagecopymerge($sImage$wImage$posX$posY, 0, 0, $wInfo[0],$wInfo[1],$alpha); 
      482.         break
      483.       //右下 
      484.       case 4: 
      485.         $posY=$sInfo[1]-$wInfo[1]; 
      486.         $posX=$sInfo[0]-$wInfo[0]; 
      487.         //生成混合圖像 
      488.         imagecopymerge($sImage$wImage$posX$posY, 0, 0, $wInfo[0],$wInfo[1],$alpha); 
      489.         break
      490.       //居中 
      491.       case 5: 
      492.         $posY=$sInfo[1]/2-$wInfo[1]/2; 
      493.         $posX=$sInfo[0]/2-$wInfo[0]/2; 
      494.         //生成混合圖像 
      495.         imagecopymerge($sImage$wImage$posX$posY, 0, 0, $wInfo[0],$wInfo[1],$alpha); 
      496.         break
      497.     } 
      498.     //如果沒有給出保存文件名,默認為原圖像名 
      499.     @unlink($source); 
      500.     //保存圖像 
      501.     imagejpeg($sImage,$source,100); 
      502.     imagedestroy($sImage); 
      503.   } 
      504. if(!function_exists('image_type_to_extension')) 
      505.   function image_type_to_extension($imagetype
      506.   { 
      507.     if(emptyempty($imagetype)) 
      508.       return false; 
      509.     switch($imagetype
      510.     { 
      511.       case IMAGETYPE_GIF  : return '.gif'
      512.       case IMAGETYPE_JPEG  : return '.jpeg'
      513.       case IMAGETYPE_PNG  : return '.png'
      514.       case IMAGETYPE_SWF  : return '.swf'
      515.       case IMAGETYPE_PSD  : return '.psd'
      516.       case IMAGETYPE_BMP  : return '.bmp'
      517.       case IMAGETYPE_TIFF_II : return '.tiff'
      518.       case IMAGETYPE_TIFF_MM : return '.tiff'
      519.       case IMAGETYPE_JPC  : return '.jpc'
      520.       case IMAGETYPE_JP2  : return '.jp2'
      521.       case IMAGETYPE_JPX  : return '.jpf'
      522.       case IMAGETYPE_JB2  : return '.jb2'
      523.       case IMAGETYPE_SWC  : return '.swc'
      524.       case IMAGETYPE_IFF  : return '.aiff'
      525.       case IMAGETYPE_WBMP  : return '.wbmp'
      526.       case IMAGETYPE_XBM  : return '.xbm'
      527.       default        : return false; 
      528.     } 
      529.   } 
      530. ?> 

      2.get_spec_img()調用圖片類,然后再用下面的方法保存不同規格的圖片并返回圖片連接

      1. //獲取相應規格的圖片地址  
      2. //gen=0:保持比例縮放,不剪裁,如高為0,則保證寬度按比例縮放 gen=1:保證長寬,剪裁  
      3. function get_spec_image($img_path,$width=0,$height=0,$gen=0,$is_preview=true)  
      4. {  
      5.   if($width==0)  
      6.     $new_path = $img_path;  
      7.   else 
      8.   {  
      9.     $img_name = substr($img_path,0,-4);  
      10.     $img_ext = substr($img_path,-3);    
      11.     if($is_preview)  
      12.     $new_path = $img_name."_".$width."x".$height.".jpg";    
      13.     else 
      14.     $new_path = $img_name."o_".$width."x".$height.".jpg";   
      15.     if(!file_exists($new_path))  
      16.     {  
      17.       require_once "imagecls.php";  
      18.       $imagec = new imagecls();  
      19.       $thumb = $imagec->thumb($img_path,$width,$height,$gen,true,"",$is_preview);  
      20.       if(app_conf("PUBLIC_DOMAIN_ROOT")!='')  
      21.       {  
      22.         $paths = pathinfo($new_path);  
      23.         $path = str_replace("./","",$paths['dirname']);  
      24.         $filename = $paths['basename'];  
      25.         $pathwithoupublic = str_replace("public/","",$path);  
      26.             $file_data = @file_get_contents($path.$file);  
      27.             $img = @imagecreatefromstring($file_data);  
      28.             if($img!==false)  
      29.             {  
      30.               $save_path = "public/".$path;  
      31.               if(!is_dir($save_path))  
      32.               {  
      33.                 @mk_dir($save_path);        
      34.               }  
      35.               @file_put_contents($save_path.$name,$file_data);  
      36.             }  
      37.       }  
      38.     }  
      39.   }  
      40.   return $new_path;  

      3.使用方法:

      1. //im:將店鋪圖片保存為3種規格:小圖:48x48,中圖120x120,大圖200x200 
      2. $small_url=get_spec_image($data['image'],48,48,0); 
      3. $<span id="result_box" class="short_text" lang="en"><span>middle_url</span></span>=get_spec_image($data['image'],120,120,0); 
      4. $big_url=get_spec_image($data['image'],200,200,0); 

      希望本文所述對大家的php程序設計有所幫助。

      分享:PHP連接操作access數據庫實例
      這篇文章主要介紹了PHP連接操作access數據庫實例,本文直接給出實現代碼,需要的朋友可以參考下 因為之前做的PingSwitch要做一個WEB展示的前端,因為一開始用了Delphi和access的結構,而Delphi與MySQL的連接又相對麻煩,最后只能選擇用PHP+Access的組合,比較奇怪,但是

      來源:模板無憂//所屬分類:PHP教程/更新時間:2015-04-01
      相關PHP教程