2015-11-04 137 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

错误在计算高度和宽度。 –

相关问题