【问题标题】:Cannot convert image into .png format in Codeigniter无法在 Codeigniter 中将图像转换为 .png 格式
【发布时间】:2015-11-04 09:37:50
【问题描述】:

我正在将图像上传到服务器。无论我以何种格式上传图像,它都应该转换为 .png 格式,但在我的情况下,它会出现以下错误,我正在使用 codeigniter。在核心 php 中它工作正常。

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

消息:imagecreatetruecolor() [function.imagecreatetruecolor]: 图片尺寸无效

消息:imagesavealpha() 期望参数 1 是资源,布尔值 给定

消息:imagecolorallocatealpha() 期望参数 1 是资源, 给定的布尔值

消息:imagefill() 期望参数 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>

【问题讨论】:

  • 您是否尝试过阅读错误以查看哪里出错了?关于imagesavealpha 的内容一开始看起来相当明显,因为您传递的是true 而不是它所期望的资源......
  • 我已经检查过它在核心 php 中工作正常,但是当我在 codeigniter 中实现我的代码时它不工作。请指导我。

标签: php image codeigniter codeigniter-2 codeigniter-3


【解决方案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);
}

注意:您可以结合使用这两种类型,但请确保提供正确的扩展名。

【讨论】:

  • Niranjan N Raju- 感谢您的回答,但我必须在上传时对其进行转换,我的上述代码在核心 php 中工作正常,但在 codeigniter 中出现以下错误。
  • 如果您遵循此方法,您不必做那么多工作。在您的代码中,您甚至将png 转换为png。我认为你应该删除它。
  • 您在哪一行收到此错误,Division by zero??
  • 这是行 $ratio_orig = $width_orig / $height_orig;我认为图像没有从表单传递到控制器。
猜你喜欢
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 2011-05-16
  • 2011-05-11
  • 2017-05-11
  • 2023-03-05
  • 2023-04-01
相关资源
最近更新 更多