Using GD to manipulate images

Hey,

I was wondering if this is possible. Someone has advised me to use GD to accomplish this.

Ok so what i want to do is insert reviews in the back end of an admin section which involves uploading an image.

See this page:

http://freemanholland.com/babies/view-reviews/?ID=1

Now would it be possible to upload the image and then also upload a cropped version of the image. So to give you an example lets say i upload this image:

http://freemanholland.com/babies/images/image.jpg

I want to upload this image, and also upload a cropped version of the image like this:

http://freemanholland.com/babies/images/cr_image.jpg

Obviously it will stay the same width but will be less in height…

Can anyone help me out? Please can you try to show any examples or tutorials etc…

Thanks

Or could it be that it does not recognize: $src = $_FILES[‘image1’][‘name’];

Hence not knowing which image to manipulate??

Do you mean like this:


    $_FILES['image1']['name'] = set_img();
	
    // Image
    $src = set_img();

:confused:

I get:

Fatal error: Call to undefined function set_img() in /home/migfreem/public_html/babies/admin/add-review.php on line 9

Ok i’ve just got back to working on this now. I am getting this error:

Warning: imagesx(): supplied argument is not a valid Image resource in /home/migfreem/public_html/babies/classes/Thumbnail.class.php on line 48

Warning: imagesy(): supplied argument is not a valid Image resource in /home/migfreem/public_html/babies/classes/Thumbnail.class.php on line 49

This is my code:


    // Image
    $src = $_FILES['image1']['name'];

    // Begin
    $img = new Thumbnail;
    $img->set_img($_FILES['image1']['tmp_name']);
    $img->set_quality(80);

    // Small thumbnail
    $img->set_size(200);
    $img->save_img("small_" . $src);
   
    // Baby thumbnail
    $img->set_size(50);
    $img->save_img("baby_" . $src);

    // Finalize
    $img->clear_cache();

    //Image 1
    $target_path1 = "../reviewimages/";
    $target_path1 = $target_path1 . basename( $_FILES['image1']['name']);

    move_uploaded_file($_FILES['image1']['tmp_name'], $target_path1);	

The image is actually uploaded perfectly. But its only the main image, the “small_” and “baby_” does not work hence why i get that error…

Can you see what i am doing wrong?

Thanks

Ok thanks Anthony.

I am following one method a user has used. Think i should show you my code so you can advise as to what i need to do :

This is my class:


<?php

class Thumbnail
{
    // Variables
    private $img_input;
    private $img_output;
    private $img_src;
    private $format;
    private $quality = 80;
    private $x_input;
    private $y_input;
    private $x_output;
    private $y_output;
    private $resize;

    // Set image
    public function set_img($img)
    {
        // Find format
        $ext = strtoupper(pathinfo($img, PATHINFO_EXTENSION));

        // JPEG image
        if(is_file($img) && ($ext == "JPG" OR $ext == "JPEG"))
        {
            $this->format = $ext;
            $this->img_input = ImageCreateFromJPEG($img);
            $this->img_src = $img;
        }

        // PNG image
        elseif(is_file($img) && $ext == "PNG")
        {
            $this->format = $ext;
            $this->img_input = ImageCreateFromPNG($img);
            $this->img_src = $img;
        }

        // GIF image
        elseif(is_file($img) && $ext == "GIF")
        {
            $this->format = $ext;
            $this->img_input = ImageCreateFromGIF($img);
            $this->img_src = $img;
        }
        
        // Get dimensions
        $this->x_input = imagesx($this->img_input);
        $this->y_input = imagesy($this->img_input);
    }

    // Set maximum image size (pixels)
    public function set_size($size = 100)
    {
        // Resize
        if($this->x_input > $size && $this->y_input > $size)
        {
            // Wide
            if($this->x_input >= $this->y_input)
            {
                $this->x_output = $size;
                $this->y_output = ($this->x_output / $this->x_input) * $this->y_input;
            }

            // Tall
            else
            {
                $this->y_output = $size;
                $this->x_output = ($this->y_output / $this->y_input) * $this->x_input;
            }   
            
            // Ready
            $this->resize = TRUE;
        }
        // Don't resize
        else { $this->resize = FALSE; }
    }

    // Set image quality (JPEG only)
    public function set_quality($quality)
    {
        if(is_int($quality))
        {
            $this->quality = $quality;
        }
    }

    // Save image
    public function save_img($path)
    {
        // Resize
        if($this->resize)
        {
            $this->img_output = ImageCreateTrueColor($this->x_output, $this->y_output);
            ImageCopyResampled($this->img_output, $this->img_input, 0, 0, 0, 0, $this->x_output, $this->y_output, $this->x_input, $this->y_input);
        }

        // Save JPEG
        if($this->format == "JPG" OR $this->format == "JPEG")
        {
            if($this->resize) { imageJPEG($this->img_output, $path, $this->quality); }
            else { copy($this->img_src, $path); }
        }

        // Save PNG
        elseif($this->format == "PNG")
        {
            if($this->resize) { imagePNG($this->img_output, $path); }
            else { copy($this->img_src, $path); }
        }

        // Save GIF
        elseif($this->format == "GIF")
        {
            if($this->resize) { imageGIF($this->img_output, $path); }
            else { copy($this->img_src, $path); }
        }
    }

    // Get width
    public function get_width()
    {
        return $this->x_input;
    }

    // Get height
    public function get_height()
    {
        return $this->y_input;
    }

    // Clear image cache
    public function clear_cache()
    {
        @ImageDestroy($this->img_input);
        @ImageDestroy($this->img_output);
    }
}
?>

Then i try using the method like this:


<?
if(isset($_POST) && count($_POST) > 0 && $_POST['title'] != ""){

	// Image
	$src = $_FILES['image1']['name'];
	
	// Begin
	$img = new Thumbnail;
	$img->set_img($src);
	$img->set_quality(80);
	
	// Small thumbnail
	$img->set_size(200);
	$img->save_img("small_" . $src);
	
	// Baby thumbnail
	$img->set_size(50);
	$img->save_img("baby_" . $src);
	
	// Finalize
	$img->clear_cache();
	
	//Image 1
    $target_path1 = "../reviewimages/";
    $target_path1 = $target_path1 . basename( $_FILES['image1']['name']);

    move_uploaded_file($_FILES['image1']['tmp_name'], $target_path1);

}

Can you see anything wrong with this? I get this error:

Warning: imagesx(): supplied argument is not a valid Image resource in /home/migfreem/public_html/babies/classes/Thumbnail.class.php on line 48

Warning: imagesy(): supplied argument is not a valid Image resource in /home/migfreem/public_html/babies/classes/Thumbnail.class.php on line 49

Any ideas?

Sure, read the user notes and documentation for [fphp]gdimagecopyresampled/fphp. :wink:

Er, no. :confused:

I mean here:-


    $img->set_img($src);

You’re not passing the filepath to the Object, just the name from what I see. Try passing $_FILES[‘image1’][‘tmp_name’] to set_img(). :slight_smile: