Hey guys,

I have a script that will resize an image, .jpg, .png or .gif

The .jpg works fine!

But the .png doesnt work at all and I just cant get it to work...

On from URL I get the following:

‰PNG  IHDR,Éᘧ

Why is this? I have a header();

Dan

Provide the code you are using, at least the PNG creation part and the serving script. Otherwise it's hard (at least for me ^_^) to give the right answer, bye.

Sure, Here it is....

<?php
ini_set("memory_limit","80M");
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
REQUIRES PARAMS:
path - path to file from domain root
w - sets width to specified value
h - sets height to specified value
scaleup - true or false
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

$file = $_GET['file'];
$q = $_GET['q'];
$w = $_GET['w'];
$h = $_GET['h'];
$colour = $_GET['colour'];
$wm = $_GET['wm'];

if (strlen($h) == 0){unset($h);};
if (strlen($w) == 0){unset($w);};
if ($q == NULL){$q = 80;};

$patharray = explode("/", $file);

$filename = $patharray[count($patharray)-1];
$path =  str_replace($filename, "", $file);
$extension = substr($filename,-4);
$filenamenoext = str_replace($extension, "", $filename);

$filestring = "";
if (isset($w)) {
$filestring = $filestring.".w.".$w;
}
if (isset($h)) {
$filestring = $filestring.".h.".$h;
}
if (isset($q)) {
$filestring = $filestring.".q.".$q;
}
if (isset($colour)) {
$filestring = $filestring.".".$colour.".".$q;
}
if (isset($wm)) {
$filestring = $filestring.".wm.".$q;
}

$filestringCach = "../image_cach/".md5($filenamenoext).$filestring.$extension;
$filestring = $path.md5($filenamenoext).$filestring.$extension;

if(file_exists($filestringCach)){
//it exists - serve up the file.......
readfile ($filestringCach);
} else {

//it doesn't exist - create the file.......
// get image size of img
$x = getimagesize($file);
// image width
$origw = $x[0];
// image height
$origh = $x[1];

if (isset ($w) && !isset ($h)) {
// autocompute height if only width is set
$factor = (100 / ($origw / $w)) * .01;
$newh = round ($origh * $factor);
$neww = $w;
} elseif (isset ($h) && !isset ($w)) {
// autocompute width if only height is set
$factor = (100 / ($origh / $h)) * .01;
$neww = round ($origw * $factor);
$newh = $h;
} elseif (isset ($h) && isset ($w)) {
// get the smaller resulting image dimension if both height and width are set
        $hx = (100 / ($origw / $w)) * .01;
        $hx = round ($origh * $hx);

        $wx = (100 / ($origh / $h)) * .01;
        $wx = round ($origw * $wx);

        if ($hx < $h) {
$factor = (100 / ($origw / $w)) * .01;
$newh = round ($origh * $factor);
$neww = $w;
        } else {
$factor = (100 / ($origh / $h)) * .01;
$neww = round ($origw * $factor);
$newh = $h;
        }
    }


if (($neww > $origw) || (($newh > $origh))){
    $newh = $origh;
    $neww = $origw;
}

if ($extension == ".jpg" || $extension == ".JPG" || $extension == "jpeg" || $extension == "JPEG"){
    $im = imagecreatefromjpeg ($file);
} elseif ($extension == ".png" || $extension == ".PNG") {
    $im = imagecreatefrompng ($file);
} elseif ($extension == ".gif" || $extension == ".GIF") {
    $im = imagecreatefromgif ($file);
}else{
    $im = false;
}

if (!$im) {
    // We get errors from PHP's ImageCreate functions...
    // So let's echo back the contents of the actual image.
    readfile($file);
} else {

    if($colour == "bw"){
        imagefilter($im, IMG_FILTER_GRAYSCALE);
    }else
    if($colour == "sepia"){
        //imagefilter($image, IMG_FILTER_GRAYSCALE);
        //imagefilter($image, IMG_FILTER_COLORIZE, 94,38, 18);
        imagefilter($im,IMG_FILTER_GRAYSCALE);
        imagefilter($im,IMG_FILTER_COLORIZE,100,50,0);
    }else
    if($colour == "neg"){
        //imagefilter($image, IMG_FILTER_GRAYSCALE);
        //imagefilter($image, IMG_FILTER_COLORIZE, 94,38, 18);
        imagefilter($im, IMG_FILTER_NEGATE);
    }   

    // Watermakr
    if($wm != NULL){
        $watermark = '../images/proof.gif';
        $wmsrc = imagecreatefromgif($watermark);
        list($markwidth, $markheight, $type1, $attr1) = getimagesize($watermark);
        // Copy and merge
        $opacity = 30;
        imagecopymerge($im, $wmsrc, ($neww-$markwidth)>>1, ($newh-$markheight)>>1, 0, 0, $markwidth, $markheight, $opacity);
    }


    // Create the resized image destination
    $thumb = imagecreatetruecolor($neww, $newh);
    if ($extension == ".png" || $extension == ".PNG"){ imagealphablending($thumb, false); }
    // Copy from image source, resize it, and paste to image destination
    imagecopyresampled($thumb, $im, 0, 0, 0, 0, $neww, $newh, $origw, $origh);
    if ($extension == ".png" || $extension == ".PNG"){ imagesavealpha($thumb, true); }

    // save resized image
    if ($extension == ".jpg" || $extension == ".JPG" || $extension == "jpeg" || $extension == "JPEG"){
        header("Content-type: image/jpeg");
        imagejpeg ($thumb, $filestringCach, $q);
    } elseif ($extension == ".png" || $extension == ".PNG") {
        header("Content-type: image/png");
        imagepng ($thumb, $filestringCach, $q);
    } elseif ($extension == ".gif" || $extension == ".GIF") {
        header("Content-type: image/gif");
        imagegif ($thumb, $filestringCach, $q);
    }

    imagedestroy($im);
    // output resized image

readfile($filestringCach);  
}
}

Thanks mate, That got all the resizing sorted, 1 small last thing, is it easy to keep anemated gifs anamated after resizing?

Thanks for you help! Dan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.