2014-08-28 57 views
0

任何人都可以告訴我,如何解決以下問題?如何jpeg圖像上傳問題在php

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /var/www/html/_controls/_images.php on line 28 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/tmp/phpaFbMJV' is not a valid JPEG file in /var/www/html/_controls/_images.php on line 28 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /var/www/html/_controls/_images.php on line 52 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /var/www/html/_controls/_images.php on line 28 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/tmp/phpaFbMJV' is not a valid JPEG file in /var/www/html/_controls/_images.php on line 28 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /var/www/html/_controls/_images.php on line 52 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /var/www/html/_controls/_images.php on line 28 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/tmp/phpUakeQQ' is not a valid JPEG file in /var/www/html/_controls/_images.php on line 28 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /var/www/html/_controls/_images.php on line 52 

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/_controls/_images.php:28) in /var/www/html/_templates/code/admin/edit_project_image.php on line 40 

我的源代碼:

<? 
class image_control 
{ 
    public $raw_image = ""; 
    public $temp_loc = ""; 

    public $image = ""; 
    public $image_resized = ""; 

    public $name = ""; 
    public $type = ""; 
    public $width = ""; 
    public $height = ""; 

    public $resize_to_width = ""; 
    public $resize_to_height = ""; 

    public $errors = array(); 

    function __construct($raw_file_post){ 
      $this->raw_image = $raw_file_post; 

      $this->temp_loc = $raw_file_post['tmp_name']; 
      $this->name = $raw_file_post['name']; 

      if($raw_file_post['type'] == "image/jpeg" || $raw_file_post['type'] == "image/pjpeg"){ 
       $this->type = 'jpeg'; 
       $this->image = imagecreatefromjpeg($this->temp_loc); 
      } 
      elseif($raw_file_post['type'] == "image/png" || $raw_file_post['type'] == "image/x-png"){ 
       $this->type = 'png'; 
       $this->image = imagecreatefrompng($this->temp_loc); 
      } 
      elseif($raw_file_post['type'] == "image/gif"){ 
       $this->type = 'gif'; 
       $this->image = imagecreatefromgif($this->temp_loc); 
      } 
      else 
       $error[] = 001; 

      list($this->width, $this->height) = getimagesize($this->temp_loc); 

      $this->image_resized = imagecreatetruecolor($this->width, $this->height); 

      imageantialias($this->image_resized, true); 

      imagealphablending($this->image_resized, false); 
      imagesavealpha($this->image_resized,true); 
      $transparent = imagecolorallocatealpha($this->image_resized, 255, 255, 255, 127); 
      imagefilledrectangle($this->image_resized, 0, 0, $this->width, $this->height, $transparent); 

      imagecopyresampled($this->image_resized, $this->image, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height); 
      $this->image = $this->image_resized; 
      $this->image_resized = ""; 
    } 

    function resize($width, $height, $resample = false){ 
      if($width !== '' && $width !== NULL && $width !== 'auto') 
       $this->resize_to_width = $width; 

      if($height !== '' && $height !== NULL && $height !== 'auto') 
       $this->resize_to_height = $height; 

      if($width == 'auto' || $height == 'auto'){ 
       if($height == 'auto') 
        $this->resize_to_height = ($this->resize_to_width/$this->width) * $this->height; 

       if($width == 'auto') 
        $this->resize_to_width = ($this->resize_to_height/$this->height) * $this->width; 
      } 

      $this->image_resized = imagecreatetruecolor($this->resize_to_width, $this->resize_to_height); 

      imageantialias($this->image_resized, true); 

      imagealphablending($this->image_resized, false); 
      imagesavealpha($this->image_resized,true); 
      $transparent = imagecolorallocatealpha($this->image_resized, 255, 255, 255, 127); 
      imagefilledrectangle($this->image_resized, 0, 0, $this->resize_to_width, $this->resize_to_height, $transparent); 

      imagecopyresampled($this->image_resized, $this->image, 0, 0, 0, 0, $this->resize_to_width, $this->resize_to_height, $this->width, $this->height); 

      if($resample == true){ 
       $this->image = $this->image_resized; 
       $this->width = $this->resize_to_width; 
       $this->height = $this->resize_to_height; 

       $this->image_resized = ""; 
       $this->resize_to_width = ""; 
       $this->resize_to_height = ""; 
      } 
    } 

    function save($folder_dir, $name){ 
      if(is_dir($folder_dir)){ 
       if($this->type == 'jpeg') 
        imagejpeg($this->image, $folder_dir . '/' . $name . '.jpg', 100) 
         or die('Image error'); 
       elseif($this->type == 'png') 
        imagepng($this->image, $folder_dir . '/' . $name . '.png') 
         or die('Image error'); 
       elseif($this->type == 'gif') 
        imagegif($this->image, $folder_dir . '/' . $name . '.gif') 
         or die('Image error'); 
      } 
      else 
       return false; 

      return true; 
    } 
} 
?> 
+1

我們可以幫助你,如果你顯示你的代碼 – 2014-08-28 10:34:05

+0

檢查GD庫是否安裝 http://php.net/manual/en/image.installation.php – Confused 2014-08-28 10:49:52

+0

@Confused,GD安裝,因爲錯誤會是不同的,像'警告:一些功能已被禁用..' – 2014-08-28 10:52:43

回答

0
/var/tmp/phpaFbMJV' is not a valid JPEG file 

phpaFbMJV顯然不是一個JPEG文件?

+0

意思是我沒有得到你,請你能清楚地告訴我。但我上傳了有效的jpeg圖像文件。 – ramkumar 2014-08-28 10:42:35

+0

@ramkumar嘗試使用'move_uploaded_file'簡單地移動圖像並下載它以查看服務器接收到的內容。 – h2ooooooo 2014-08-28 10:45:49

+0

你錯過了你的問題的一些代碼。 '$ raw_file_post'數組的內容是什麼? – 2014-08-28 10:46:55

0

您提供的文件不是有效的請檢查文件。只有擴展名.jpg不一定是有效的jpg格式的圖像。

0

只要內容類型的頭文件很容易被某些用戶試圖查找漏洞攻擊覆蓋,我建議您使用imagecreatefromstring打開文件。

$this->image = @imagecreatefromstring(@file_get_contents($this->temp_loc)); 
if ($this->image === false) { 
    throw new Exception("File is unreachable or not a supported image"); 
} 

接下來,您要在保存圖像格式的同時保存它們。我建議你只使用一種格式,比如png,所以你的所有圖像現在都是相同的類型,你不需要擔心這個問題了。