2012-12-24 25 views
4

我實際上正在嘗試將圖像製作成三種尺寸,然後使用不同的名稱將它們存儲在服務器文件夾中。我通過移動客戶端獲取Base64編碼格式的圖像數據。這裏是我試圖將圖像轉換爲三個圖像的代碼。在PHP中將圖像劃分爲三種尺寸

這是我正在接收數據的PHP文件。

$uploadPath = 'C:/xampp/htdocs/OSFiles/of-images/images/'; 

if(!is_dir($uploadPath)) 
    mkdir($uploadPath,true) or trigger_error("Can't Create Folder"); 

$file = tempnam($uploadPath, 'image'); 
$fp  = fopen($file, 'wb'); 
fwrite($fp, $binary); //$binary is--> I am reading the image in binary as I am getting the photo data in base64 encoded format through mobile client side. So I decoded it to binary. 

fclose($fp); 

$result1 = mysql_query("INSERT INTO oc_t_item_resource(fk_i_item_id, s_name,s_extension,s_content_type, s_path) 
     VALUES('$lastid','$s_name','$extension','$s_content_type','$imagepath')"); 

$imagelastid = mysql_insert_id(); 

// Create normal size 
$normal_path = $path = $uploadPath . $imagelastid . '.jpg' ; 
$size = explode('x', '640x480') ; 

ImageResizer::fromFile($file)->resizeTo($size[0], $size[1])->saveToFile($path) ; 

// Create preview 
$path = $uploadPath . $imagelastid . '_preview.jpg' ; 
$size = explode('x', '480x340') ; 
ImageResizer::fromFile($file)->resizeTo($size[0], $size[1])->saveToFile($path) ; 

// Create thumbnail 
$path = $uploadPath . $imagelastid . '_thumbnail.jpg' ; 
$size = explode('x', '240x200') ; 
ImageResizer::fromFile($file)->resizeTo($size[0], $size[1])->saveToFile($path) ; 

這是我ImageResizer類,這是在ImageResizer.php文件。

<?php 
    class ImageResizer { 

     public static function fromFile($imagePath) { 
      return new ImageResizer($imagePath); 
     } 

     private $im; 

     private function __construct($imagePath) { 
      if(!file_exists($imagePath)) throw new Exception("$imagePath does not exist!"); 
      if(!is_readable($imagePath)) throw new Exception("$imagePath is not readable!"); 
      if(filesize($imagePath)==0) throw new Exception("$imagePath is corrupt or broken!"); 

      if(osc_use_imagick()) { 
       $this->im = new Imagick($imagePath); 
      } else { 
       $content = file_get_contents($imagePath); 
       $this->im = imagecreatefromstring($content); 
      } 

      return $this; 
     } 

     public function __destruct() { 
      if(osc_use_imagick()) { 
       $this->im->destroy(); 
      } else { 
       imagedestroy($this->im); 
      } 
     } 

     public function resizeTo($width, $height) { 
      if(osc_use_imagick()) { 
       $bg = new Imagick(); 
       $bg->newImage($width, $height, 'white'); 

       $this->im->thumbnailImage($width, $height, true); 
       $geometry = $this->im->getImageGeometry(); 

       $x = ($width - $geometry['width'])/2; 
       $y = ($height - $geometry['height'])/2; 

       $bg->compositeImage($this->im, imagick::COMPOSITE_OVER, $x, $y); 
       $this->im = $bg; 
      } else { 
       $w = imagesx($this->im); 
       $h = imagesy($this->im); 

       if(($w/$h)>=($width/$height)) { 
        //$newW = $width; 
        $newW = ($w > $width)? $width : $w; 
        $newH = $h * ($newW/$w); 
       } else { 
        //$newH = $height; 
        $newH = ($h > $height)? $height : $h; 
        $newW = $w * ($newH/$h); 
       } 

       $newIm = imagecreatetruecolor($width,$height);//$newW, $newH); 
       imagealphablending($newIm, false); 
       $colorTransparent = imagecolorallocatealpha($newIm, 255, 255, 255, 127); 
       imagefill($newIm, 0, 0, $colorTransparent); 
       imagesavealpha($newIm, true); 
       imagecopyresampled($newIm, $this->im, (($width-$newW)/2), (($height-$newH)/2), 0, 0, $newW, $newH, $w, $h); 
       imagedestroy($this->im); 

       $this->im = $newIm; 
      } 
      return $this; 
     } 

     public function saveToFile($imagePath) { 
      if(file_exists($imagePath) && !is_writable($imagePath)) throw new Exception("$imagePath is not writable!"); 
      if(osc_use_imagick()) { 
       $this->im->setImageFileName($imagePath); 
       $this->im->writeImage($imagePath); 
      } else { 
       imagejpeg($this->im, $imagePath); 
      } 
     } 

     public function show() { 
      header('Content-Disposition: Attachment;filename=image.jpg'); 
      header('Content-type: image/jpg'); 
      if(osc_use_imagick()) { 
      } else { 
       imagepng($this->im); 
      } 
     } 
    } 
?> 

圖像沒有得到存儲在圖像文件夾中。我哪裏錯了?

帶有更正的代碼片段將不勝感激。

(我是新來PHP,所以請溫柔。)

+1

任何錯誤信息?你可以在沒有調整大小的步驟上傳文件嗎? – ethrbunny

+0

@ethrbunny是的。如果我通過重命名臨時圖像,我可以在不調整大小的情況下上傳圖像。請建議。 – rick

+0

好吧 - 那麼您繞過哪些步驟才能使其工作? – ethrbunny

回答

1

我建議竊聽器搜索時使用error_reporting(-1)。然後,您只需按照消息並修復所有錯誤。

您跳過mkdir的一個參數。 $ mode在Windows上被忽略,但它仍然應該被傳遞以獲得$ recursive參數:mkdir($uploadPath, 0777, true)

此外,你應該有一個功能osc_use_imagick包括在某處。我沒有它,所以我不得不將條件從if(osc_use_imagick())改爲if(function_exists('osc_use_imagick') && osc_use_imagick())

順便說一句,你忘了在所有調整大小後刪除臨時文件。將unlink($file)添加到腳本的末尾。