dsmnedc798226 2015-11-04 09:37
浏览 39

无法在Codeigniter中将图像转换为.png格式

I am uploading image to server. In whatever format i upload the image it should be converted to .png format but in my case it is giving following error and i am working on codeigniter. In core php it is working properly.

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

Message: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions

Message: imagesavealpha() expects parameter 1 to be resource, boolean given

Message: imagecolorallocatealpha() expects parameter 1 to be resource, boolean given

Message: imagefill() expects parameter 1 to be resource, boolean given

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

Message: imagepng() expects parameter 1 to be resource, boolean given

Controller:

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');
}

This is my form:

<?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>
  • 写回答

1条回答 默认 最新

  • douchilian1009 2015-11-04 09:42
    关注

    First upload the any format to some dummy folder and then try changing the format of the file to 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);
    }
    

    NOTE: you can combine both types, but make sure you give correct extention.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题