Welcome Guest, Not a member yet? Register   Sign In
Image Manipulation - Convert
#1

Hello,

I'm trying to convert PNG to JPG using Image Manipulation Class

PHP Code:
\Config\Services::image()
->
withFile($file//$file is PNG image 
->convert(IMAGETYPE_JPEG)
->
save($jpg); 

but it always show me error "Unable to save the image. Please make sure the image and file directory are writable". I've triple check permission folder but there's no problem with it. I also try with common way below with no problem


PHP Code:
$new_pic imagecreatefrompng($file);
// Create a new true color image with the same size
$w imagesx($new_pic);
$h imagesy($new_pic);
$white imagecreatetruecolor($w$h);

// Fill the new image with white background
$bg imagecolorallocate($white255255255);
imagefill($white00$bg);

// Copy original transparent image onto the new image
imagecopy($white$new_pic0000$w$h);

$new_pic $white;

imagejpeg($new_pic$jpg);
imagedestroy($new_pic); 

I tried to trace to CI4 GDHandler.php and in save() I tried to var_dump($this->resource) but it return NULL.

PHP Code:
case IMAGETYPE_JPEG:
    if (! 
function_exists('imagejpeg'))
    {
        throw 
ImageException::forInvalidImageCreate(lang('images.jpgNotSupported'));
    }
        
        
var_dump($this->resource); //NULL
        
die('finish');

    if (! @
imagejpeg($this->resource$target$quality))
    {
        throw 
ImageException::forSaveFailed();
    }
    break; 
Reply
#2

Hi, this aren't possible at this time with the current stable version. The resource are only available if you do some image manipulation.

It will work with withResource in the next version.
https://github.com/codeigniter4/CodeIgniter4/pull/2963

You can however use procedure style at the moment.
https://github.com/codeigniter4/CodeIgni...t.php#L372
Reply
#3

(This post was last modified: 06-24-2020, 11:43 PM by inumaru.)

(06-22-2020, 10:27 AM)jreklund Wrote: Hi, this aren't possible at this time with the current stable version. The resource are only available if you do some image manipulation.

It will work with withResource in the next version.
https://github.com/codeigniter4/CodeIgniter4/pull/2963

You can however use procedure style at the moment.
https://github.com/codeigniter4/CodeIgni...t.php#L372

So you cannot just convert an image but need to do some other manipulation first?

PHP Code:
$image->withFile($file->getTempName())
                    ->convert(IMAGETYPE_JPEG)
                 ->
save("./folder_path/image_name.jpg");   --> result in exception 


$image
->withFile($file->getTempName())
                    ->flatten(255,255,255)
                    ->convert(IMAGETYPE_JPEG)
                    ->
save("./folder_path/image_name.jpg"); --> success no exception returned 

something like that?
Reply
#4

In the current state you can't just convert the image, you need to do at least one manipulation, or use procedure style coding. I don't have an release date for when you can do it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB