Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts

    Convert jpg to png question

    Gidday

    I can successfully upload a jpeg image from a Flash AIR app to my server:

    Code:
    $filename = $_FILES['Filedata']['name'];
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    
    if ($extension == 'jpg')
    { 	
    	if(!move_uploaded_file($_FILES['Filedata']['tmp_name'], $filename))
    	{	
    		$erMsg = "img_save_fail";
    		sendError($erMsg);				
    	}
    }
    I'm now trying to convert the jpg to a png8 before saving it, but haven't cracked it yet.

    This is my latest code:

    Code:
    $filename = $_FILES['Filedata']['name'];
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    
    if ($extension == 'jpg')
    { 	
    	$im = imagecreatefromjpeg($filename);
    	$im = imagepng($im);
    
            if(!move_uploaded_file($im, $filename))
    	{	
    		$erMsg = "img_save_fail";
    		sendError($erMsg);				
    	}
    }
    Am I close? Can someone see the error here?

    Cheers

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Don't use move_uploaded_file here. I'm not sure what the system will do in consideration to the is_uploaded_file. I would presume no matter what you do, it will not pass that check and therefore will not work in a move.
    Verify your image first, and then simply save it to the location given that as the second paramter to the imagepng. $extension here is optional of course; the extension doesn't dictate the type, but if imagecreatefromjpeg fails, then the image isn't a valid jpeg image type. You can use that to your favour:
    PHP Code:
    $sSavePath '/path/to/where/you/save/file/';
    $sFilename pathinfo($_FILES['Filedata']['name'], PATHINFO_FILENAME);
    if (
    $im = @imagecreatefromjpeg($_FILES['Filedata']['tmp_name']))
    {
        if (!
    imagepng($imrtrim($sSavePath'/') . '/' $sFilename '.png'))
        {
            
    sendError('Failed to save as PNG');
        }
        
    imagedestroy($im);
        
    unlink($_FILES['Filedata']['tmp_name']);
    }
    else
    {
        
    sendError('That is not a valid JPEG image');

    That assumes you don't want to keep the JPEG as well.

  • Users who have thanked Fou-Lu for this post:

    shaunthomson (11-30-2012)

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts
    Thanks Fou-Lu - only got to this today. Works a charm. Much appreciated.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •