resize image problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

resize image problem

Post 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.
User avatar
phpcip28
Forum Newbie
Posts: 22
Joined: Sun Aug 29, 2010 1:38 pm
Location: NewYork
Contact:

Re: resize image problem

Post 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);
    }
Post Reply