Page 1 of 1

resize image problem

Posted: Sat Aug 28, 2010 11:25 am
by adsegzy
Hello friends,

please am having problem with my image resize code the code is as below;

Code: Select all

$originalImage = $_FILES["file"]["name"];
$toWidth = 300;
$toHeight = 300;

function resizeImage($originalImage,$toWidth,$toHeight){
    
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;
    
    // Recalculate new size with default ratio
    if ($yscale>$xscale){
        $new_width = round($width * (1/$yscale));
        $new_height = round($height * (1/$yscale));
    }
    else {
        $new_width = round($width * (1/$xscale));
        $new_height = round($height * (1/$xscale));
    }

    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp     = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    return $imageResized;
}
move_uploaded_file($_FILES["file"]["tmp_name"],
		"../upload/" . $imageResized);
		$path2= "../upload/" . $imageResized;
but the picture is not resized, and if i use

Code: Select all


$image = resizeImage()
move_uploaded_file($_FILES["file"]["tmp_name"],
		"../upload/" . $image);
		$path2= "../upload/" . $image;
no picture will be uploaded to my upload folder.

please what can i do or which other lesser stress way can i resize my picture.

Re: resize image problem

Posted: Sun Aug 29, 2010 2:34 pm
by phpcip28
I have written this method in my Image class that works like a charm.
IMPORTANT: Please note that this will output the image to the screen so you need to call this function inside a file like this:
Let's say you create thumb.php and put that function in there and than you have image.php where you have this:

Also note that this works for most image file types and will also preserve transparency for png and gif images. Not sure if you need that, but you can remove it from the end there if you don't..
Also note that this will scale the image to a width of your own choice, which I called it $scaled_size_in_pixels here.

Code: Select all

require_once("thumb.php");
echo thumb("/path/to/your/image.jpeg", $the_scaled_size_in_pixels);
And then in your html template file you go like:

Code: Select all

<img src="image.php" />
Of course you could send the path via $_GET like this

Code: Select all

<img src="image.php?image_file=/path/to/image/file" />
I'm sure you get the drill.

Here's the function

Code: Select all

function thumb($image_file , $sz ="-1", $quality = "75"){

        $this->image_file = $image_file;
        $this->sz = $sz;
        $this->quality = $quality;


        $imgDat = @getimagesize($this->image_file);
        $mime = $imgDat['mime'];

        if ($imgDat[0] > $imgDat[1]+20) {
          $tw = $this->sz;
          $th = $imgDat[1]/($imgDat[0]/$tw);
        } else {
          $th = $this->sz;
          $tw = $imgDat[0]/($imgDat[1]/$th);
        }

        if ($this->sz == "-1") {
            $imNew = ImageCreateTrueColor($imgDat[0], $imgDat[1]);
        }else{
            $imNew = ImageCreateTrueColor($tw, $th);
        }

        if ($mime == 'image/gif') {
          $imFile= ImageCreateFromGIF($this->image_file);
        } else if ($mime == 'image/png') {
          $imFile= ImageCreateFromPNG($this->image_file);
        } else if ($mime == 'image/jpeg') {
          $imFile= ImageCreateFromJpeg($this->image_file);
        }

        if ($this->sz == "-1") {
          $res = imagecopyresampled ($imNew, $imFile, 0, 0, 0, 0, $imgDat[0], $imgDat[1], $imgDat[0], $imgDat[1]);
        } else {
          $res = imagecopyresampled ($imNew, $imFile, 0, 0, 0, 0, $tw, $th, $imgDat[0], $imgDat[1]);
        }




        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        header("Content-Type: ".$mime);
        header('Content-Disposition: inline; filename=test');



        if ($mime == 'image/gif') {
          imagealphablending($imNew, false);
          imagesavealpha($imNew,true);
          $transparent = imagecolorallocate($imNew, 0, 0, 0);
          imagecolortransparent($imNew, $transparent);

          $imFile= ImageCreateFromGIF($this->image_file);
          imagegif($imNew);
        } else if ($mime == 'image/png') {
          imagealphablending($imNew, false);
          imagesavealpha($imNew,true);
          $transparent = imagecolorallocate($imNew, 0, 0, 0);
          imagecolortransparent($imNew, $transparent);

          $imFile= ImageCreateFromPNG($this->image_file);
          imagepng($imNew);
        } else if ($mime == 'image/jpeg') {
          $imFile= ImageCreateFromJpeg($this->image_file);
          imagejpeg($imNew);
        }


        imagedestroy($imNew);
    }