1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Transparent gif images do not upload after rotation

Discussion in 'PHP' started by qwikad.com, Jan 11, 2020.

  1. #1
    @deathshadow and others...

    I am working on a script that allows to rotate images before upload. All images load up fine except transparent gif images. They load fine if not rotated, but when rotated they do not show up in the upload folder. The $source echos empty. I am thinking the issue is somewhere here:

    
    ......
    
    $allowTypes = array('image/png','image/jpg','image/jpeg','image/gif');
    
    ......
    
    if(!empty($rotation)){
    switch($fileType){
    case 'image/png':
    $source = imagecreatefrompng($fileTemp);
    break; 
    case 'image/gif':
    $source = imagecreatefromgif($fileTemp);
    break;
    default:
    $source = imagecreatefromjpeg($fileTemp);
    }
    $imageRotate = imagerotate($source, $rotation,0);
    
    switch($fileType){
    case 'image/png':
    $upload = imagepng($imageRotate, $filePath);
    break;
    case 'image/gif':
    $upload = imagegif($imageRotate, $filePath);
    break;
    default:
    $upload = imagejpeg($imageRotate, $filePath);
    }
    }
    
    
    Code (markup):
    Is there something I am missing?
     
    Solved! View solution.
    qwikad.com, Jan 11, 2020 IP
  2. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #2
    Your code is working perfect for me.
    What is the rotation angle you are using?
    People have reported bugs here when using certain angles
    https://www.php.net/manual/en/function.imagerotate.php

    Can you provide the image you are using?
    Don't attach to a comment here. Upload on your server and post link to gif please.

    Can you run this code:


    //source file name
    $fileTemp='animal.gif';

    //destination file name
    $filePath = 'animal11.gif';

    $source = imagecreatefromgif($fileTemp);
    if( !$source ){ echo "no image"; }else{ echo "got image"; }

    $imageRotate = imagerotate($source, 180, 0);
    if( !$imageRotate ){ echo " no rotation"; }else{ echo " Done rotation"; }

    $upload = imagegif($imageRotate, $filePath);
     
    JEET, Jan 11, 2020 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #3
    qwikad.com, Jan 11, 2020 IP
  4. #4
    Try to use this method to read $source file:

    $source= imagecreatefromstring( file_get_contents( $fileTemp));


    That "imagecreatefromgif" is not recognizing this as valid GIF image.
    The above method worked for me.
     
    JEET, Jan 11, 2020 IP
    scu8a likes this.
  5. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    Its happening because image is not a GIF image. Its a PNG image. The gif extension is a mistake...
     
    JEET, Jan 11, 2020 IP
  6. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #6
    So out of everything I could have chosen I chose a wrong image... Hmm... I am going to test it with a different image to see if the issue isn't there.
     
    qwikad.com, Jan 11, 2020 IP
  7. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #7
    This is working. Thank you. I tried gif animated images, still images and that image that didn't work, all of them got uploaded.
     
    qwikad.com, Jan 11, 2020 IP
    JEET and scu8a like this.
  8. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #8
    Welcome, no problem! :)
     
    JEET, Jan 11, 2020 IP