Page 1 of 1

crop & upload PNG pictures are having black background

Posted: Mon Sep 08, 2014 7:48 am
by adsegzy
Hello,

Pls is anything wrong with my script below? when ever I upload & crop a PNG image, it comes with black background, what do I do or which crop, resize & upload script is better?

Code: Select all

<?php
// Adam Khoury PHP Image Function Library 1.0
// -------------- RESIZE FUNCTION -------------
// Function for resizing any jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
    list($w_orig, $h_orig) = getimagesize($target);
    $scale_ratio = $w_orig / $h_orig;
    if (($w / $h) > $scale_ratio) {
           $w = $h * $scale_ratio;
    } else {
           $h = $w / $scale_ratio;
    }
    $img = "";
    $ext = strtolower($ext);
    if ($ext == "gif"){ 
    $img = imagecreatefromgif($target);
    } else if($ext =="png"){ 
    $img = imagecreatefrompng($target);
    } else { 
    $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    // imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
    imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
    if ($ext == "gif"){ 
        imagegif($tci, $newcopy);
    } else if($ext =="png"){ 
        imagepng($tci, $newcopy);
    } else { 
        imagejpeg($tci, $newcopy, 84);
    }
}
// ------------- THUMBNAIL (CROP) FUNCTION -------------
// Function for creating a true thumbnail cropping from any jpg, gif, or png image files
function ak_img_thumb($target, $newcopy, $w, $h, $ext) {
    list($w_orig, $h_orig) = getimagesize($target);
    $src_x = ($w_orig / 2) - ($w / 2);
    $src_y = ($h_orig / 2) - ($h / 2);
    $ext = strtolower($ext);
    $img = "";
    if ($ext == "gif"){ 
    $img = imagecreatefromgif($target);
    } else if($ext =="png"){ 
    $img = imagecreatefrompng($target);
    } else { 
    $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    imagecopyresampled($tci, $img, 0, 0, $src_x, $src_y, $w, $h, $w, $h);
    if ($ext == "gif"){ 
        imagegif($tci, $newcopy);
    } else if($ext =="png"){ 
        imagepng($tci, $newcopy);
    } else { 
        imagejpeg($tci, $newcopy, 84);
    }
}
?>

Re: crop & upload PNG pictures are having black background

Posted: Mon Sep 08, 2014 8:14 am
by Celauran
I generally use Imagine for image manipulation.

Re: crop & upload PNG pictures are having black background

Posted: Mon Sep 08, 2014 10:34 am
by phpdeveloper1
adsegzy wrote:Hello,

Pls is anything wrong with my script below? when ever I upload & crop a PNG image, it comes with black background, what do I do or which crop, resize & upload script is better?
Are gif and jpg working ?

Re: crop & upload PNG pictures are having black background

Posted: Tue Sep 09, 2014 4:47 pm
by adsegzy
Thank Guys,
@ Celauran, i will give it a try

@ hpdeveloper1, it's working perfectly with jpg but haven't tried gif