1

I am passing an image through 2 functions: a(), an image resizer function & b(), an image color bit function. After b() is done I'm passing it back to a(), yet after b() handled the image, I am no longer getting any returned value from a() anymore, assuming b() function is the culprit here, I'm trying to see where I went wrong here.

function a();

function image_resize($src, $dst, $width, $height, $crop=0){

    if(!list($w, $h) = getimagesize($src)) return "Unsupported picture type!";

    $type = strtolower(substr(strrchr($src,"."),1));
    if($type == 'jpeg') $type = 'jpg';
    switch($type){
        case 'bmp': $img = imagecreatefromwbmp($src); break;
        case 'gif': $img = imagecreatefromgif($src); break;
        case 'jpg': $img = imagecreatefromjpeg($src); break;
        case 'png': $img = imagecreatefrompng($src); break;
        default : return "Unsupported picture type!";
    }

    // resize
    if($crop){
        if($w < $width or $h < $height) return "Picture is too small!";
        $ratio = max($width/$w, $height/$h);
        $h = $height / $ratio;
        $x = ($w - $width / $ratio) / 2;
        $w = $width / $ratio;
    }
    else{
        if($w < $width and $h < $height) return "Picture is too small!";
        $ratio = min($width/$w, $height/$h);
        $width = $w * $ratio;
        $height = $h * $ratio;
        $x = 0;
    }

    $new = imagecreatetruecolor($width, $height);

    // preserve transparency
    if($type == "gif" or $type == "png"){
        imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
        imagealphablending($new, false);
        imagesavealpha($new, true);
    }

    imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
    switch($type){
        case 'bmp': imagewbmp($new, $dst); break;
        case 'gif': imagegif($new, $dst); break;
        case 'jpg': imagejpeg($new, $dst); break;
        case 'png': imagepng($new, $dst); break;
    }
    return true;
}

function b();

function convertJPGto8bitJPG($sourcePath, $destPath) {
    $srcimage = imagecreatefromjpeg ($sourcePath);
    list($width, $height) = getimagesize($sourcePath);
    $img = imagecreatetruecolor($width, $height);
    $bga = imagecolorallocatealpha($img, 0, 0, 0, 127);
    imagecolortransparent($img, $bga);
    imagefill($img, 0, 0, $bga);
    imagecopy($img, $srcimage, 0, 0, 0, 0, $width, $height);
    imagetruecolortopalette($img, false, 255);
    imagesavealpha($img, true);
    imagejpeg($img, $destPath);
    //imagedestroy($img);
    return $destPath;
}

Please don't ask 'why not imagemagick..', with the overwhelming and unpalatable plethora of diff versions for xampp I just gave up on imagemagick and GD is just fine, if you have any recommendation on libraries for GD and a quick resolve for this mess, that would be mostly appreciative.

clusterBuddy
  • 1,523
  • 12
  • 39
  • Imagemagick worked for me without problems everytime I installed it on XAMPP. However if you are talking about Imagick, I just gave up with it. – Bonzo Oct 24 '16 at 10:05
  • @Bonzo - what would be the main diff. between using each of them? thx – clusterBuddy Oct 24 '16 at 12:29
  • Imagemagick is the original program. Imagick is a php API for Imagemagick. I use Imagemagick with the command line and exec(). Imagick also will not support all the options available in Imagemagick. If you install Imagemagick on a windows PC it is also available though the command line and batch files. Similarly for Linux and mac. There are security concerns using Imagemagick with exec() which Imagick is supposed to overcome. I think if you validate any user input correctly you should be safe from most hacks. – Bonzo Oct 24 '16 at 14:26

0 Answers0