2015-11-04 68 views
0

我正在將圖像上傳到服務器。以任何格式我上傳圖像它應該被轉換爲.png格式,但在我的情況下,它給出了以下錯誤,我正在處理codeigniter。在覈心的PHP它正在正常工作。無法在Codeigniter中將圖像轉換爲.png格式

Message: Division by zero 
Filename: controllers/Upload_user_image.php 

消息:imagecreatetruecolor()[function.imagecreatetruecolor]: 無效圖像尺寸

消息:imagesavealpha()預計參數1是資源,布爾 給出

消息:imagecolorallocatealpha ()期望參數1是資源, 布爾給定

消息:image補()預計參數1是資源,布爾給出

Message: imagecopyresampled() expects parameter 1 to be resource, boolean given 
> Message: Undefined variable: c_image 

消息:imagepng()預計參數1是資源,布爾給出

控制器:

function do_upload() 
{ 
     $cimg = $_FILES['user_file']['tmp_name']; 
     $extension = pathinfo($cimg, PATHINFO_EXTENSION); 
     $srcFile_c = $_FILES['user_file']['tmp_name']; 
     /*function imageToPng($srcFile, $maxSize = 100) {*/                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     list($width_orig, $height_orig, $type) = getimagesize($srcFile_c); 
     $maxSize = 100; 
     // Get the aspect ratio 
     $ratio_orig = $width_orig/$height_orig; 

     $width = $maxSize; 
     $height = $maxSize;                

     // resize to height (orig is portrait) 
     if ($ratio_orig < 1) 
     { 

      $width = $height * $ratio_orig; 

     } 
     // resize to width (orig is landscape) 
     else 
     { 
      $height = $width/$ratio_orig; 
     } 

     // Temporarily increase the memory limit to allow for larger images 
     ini_set('memory_limit', '32M'); 


     $u_name='pawan'; 
     switch (@$type) 
     { 
     case IMAGETYPE_GIF: 
     $c_image = imagecreatefromgif($srcFile_c); 
     break; 
     case IMAGETYPE_JPEG: 
     $c_image = imagecreatefromjpeg($srcFile_c); 
     break; 
     case IMAGETYPE_PNG: 
     $c_image = imagecreatefrompng($srcFile_c); 
     break; 
     default: 
     throw new Exception('Unrecognized image type ' . @$type); 
     }  

     // create a new blank image 
     $new_c_Image = imagecreatetruecolor($width, $height); 
     imagesavealpha($new_c_Image, true); 
     $trans_colour = imagecolorallocatealpha($new_c_Image, 0, 0, 0, 127); 
     imagefill($new_c_Image, 0, 0, $trans_colour); 
     imagecopyresampled($new_c_Image, $c_image, 0, 0, 0, 0, $width, $height, $width_orig,$height_orig); 
     $getpng = imagepng($new_c_Image, './uploads/' . "img_" . $u_name . '.png'); 
} 

這是我的表單:

<?php echo form_open_multipart('Upload_user_image/do_upload');?> 

<input type="file" name="userfile" /> 

<input type="text" name="password"/> 
<input type="submit" value="upload" /> 

</form> 
+0

您是否嘗試過閱讀錯誤以查看錯誤發生的位置?關於'imageavealpha'的一個看起來相當明顯的一開始,因爲你傳遞'true'而不是它所期望的資源... – Jonnus

+0

我檢查過它在覈心php中工作正常,但是當我在codeigniter中實現我的代碼時不管用。請指導我。 – user3653474

回答

1

首先上傳任意格式化爲某個虛擬文件夾,然後嘗試將文件格式更改爲png

if ($imageFileType == "jpg") { 
    $jpg = "path to dummy jpg image folder"; 
    imagepng(imagecreatefromstring(file_get_contents($jpg)), "path where you want to store png images"); 
    unlink($jpg); 
} else if ($imageFileType == "jpeg") { 
    $jpg = "path to dummy jpeg image folder"; 
    imagepng(imagecreatefromstring(file_get_contents($jpg)),"path where you want to store png images"); 
    unlink($jpg); 
} 

注:你可以結合這兩種類型,但一定要給出正確的推廣。

+0

Niranjan N Raju-感謝您的回答,但我只能在上傳時將其轉換,我的上面的代碼在覈心php中工作正常,但它在codeigniter中給出了以下錯誤。 – user3653474

+0

如果你遵循這個方法,你不必做那麼多工作。而在你的代碼中,你甚至會把'png'轉換成'png'。我認爲你應該刪除它。 –

+0

錯誤在計算高度和寬度。 –