Jump to content

Image Resizing


martinm_92

Recommended Posts

Having a nightmare here lol :P

 

$img->set_size(225);

 

That is resizing the image with a maximum width but i need to resize it to make it have a maximum height but can't figure out how to do it :(

 

Here's my full code if you want to see it:

 

<?php
include("class.imaging.php");

// The file
$dir = "upload/";
$file = $newfile;
$filename = $dir . $file;


$img = new imaging;
$img->set_img($filename);
$img->set_quality(120);

// Profile Picture
$img->set_size(225);
$img->save_img("upload/thumb/" . $file);

$img->clear_cache();
?>

 

Link to comment
Share on other sites

Ok its a bit long.

<?php

// Imaging
class imaging
{

    // Variables
    private $img_input;
    private $img_output;
    private $img_src;
    private $format;
    private $quality = 120;
    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);
		imagesavealpha($this->img_output, true);
		$trans_colour = imagecolorallocatealpha($this->img_output, 0, 0, 0, 127);
   			imagefill($this->img_output, 0, 0, $trans_colour);
            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);

    }

}
?>

Link to comment
Share on other sites

here is the resize function I use...

<?php 
function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) { 
$s_path = trim($s_path); 
$o_path = trim($o_path); 
$save = $s_path . $save; 
$file = $o_path . $file; 
$ext = strtolower(end(explode('.',$save))); 
list($width, $height) = getimagesize($file) ; 
if(($width>$t_w) OR ($height>$t_h)) { 
	$r1 = $t_w/$width; 
	$r2 = $t_h/$height; 
	if($r1<$r2) { 
		$size = $t_w/$width; 
	}else{ 
		$size = $t_h/$height; 
	} 
}else{ 
	$size=1; 
} 
$modwidth = $width * $size; 
$modheight = $height * $size; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
switch ($ext) { 
	case 'jpg': 
	case 'jpeg': 
		$image = imagecreatefromjpeg($file) ; 
		break; 
	case 'gif': 
		$image = imagecreatefromgif($file) ; 
		break; 
	case 'png': 
		$image = imagecreatefrompng($file) ; 
		break; 
} 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
imagejpeg($tn, $save, 100) ; 
return; 
} 
/* end of function */

/*

Example usage 

*/

$save = 'myfile.jpg'; /* set original file name here */
$file = $newfile; /* set new file name here */
$t_w = 120; /* set max thumb width here */
$t_h = 120; /* set max thumb height here */
$o_path = "upload/"; /* set path to original file here */ 
$s_path = "upload/thumb/"; /* set path to new file here */

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); 
?> 

Link to comment
Share on other sites

Oh this is much better thanks mate :D Is it possible to resize the image twice? So I can have a smaller 50x50 thumbnail image? Im pretty sure that resizing and cropping to a square image is a whole different thing but can I add another part to the form that resizes a new image with a max height and width of 50?

Link to comment
Share on other sites

Sorry I meant how do i do multiple resizing :P

 

Is it something like:

 

<?php
include("class.imaging.php");

$file = $newfile;
$filename = $dir . $file;

/* Profile Picture */
$save = $filename;
$t_w = 380;
$t_h = 137;
$o_path = "upload/"; 
$s_path = "upload/newfile/";

/* Thumbnail */
$save .= $filename;
$t_w .= 50;
$t_h .= 50;
$o_path .= "upload/"; 
$s_path .= "upload/thumb/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);
?>

 

I've tested that and it doesn't work... but is it something similar?

Link to comment
Share on other sites

/* Profile Picture */
$file = SOME IMAGE; /* this the name of the ORIGINAL image WITHOUT path info*/
$save = $filename; /* This is the name you want to save as WITHOUT path info */
$t_w = 380;
$t_h = 137;
$o_path = "upload/";  /* This is the path to the ORIGINAL image */
$s_path = "upload/newfile/"; /* This is the path where you want the resized image saved */
Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);


/* Thumbnail */
/* this will take the image saved in the above code and create a thumb in a different folder */
/* being as they are in different folders, we can use the same name, just different pathes */
/* that way you only need to enter the name in your database once. You will adjust your */
/* code to determine which version you are displaying */
$t_w = 50;
$t_h = 50;
$o_path = "upload/newfile/"; 
$s_path = "upload/thumb/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);


Link to comment
Share on other sites

<form name="form_upload" method="POST" enctype="multipart/form-data">
<table width="100%" cellpadding="0" cellspacing="3" border="0">
<tr>
    	<td colspan="2"><h1>Upload Profile Picture</h1></td>
    </tr>
    <tr>
    	<td align="right"><p>Image title:</p></td>
        <td align="left"><input type="text" name="title" class="editinput" /></td>
    </tr>
    <tr>
    	<td align="right"><p>Upload:</p></td>
        <td align="left"><input type="file" name="file" class="editinput" /></td>
    </tr>
    <tr>
    	<td> </td>
        <td><input type="submit" value="Upload" class="editSubmit" name="upload" /></td>
    </tr>
    <tr>
    	<td> </td>
    	<td>
        <?php
        if($_POST['upload']){
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			if(($_FILES['file']['type'] == "image/gif")
			|| ($_FILES['file']['type'] == "image/jpeg")
			|| ($_FILES['file']['type'] == "image/png")
			|| ($_FILES['file']['type'] == "image/pjpeg")){
				if($_FILES['file']['error'] > 0){
					echo "Return Code: " . $_FILES['file']['error'] . "<br>";
				}else{
					$title = $_POST['title'];
					$oldfile = $_FILES['file']['name'];
					$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

					$newfile = md5(time()) . "." . $ext;
					$thumb = md5(time()) . "." . $ext;

					if(move_uploaded_file($_FILES['file']['tmp_name'], "./upload/".$newfile)){
						mysql_query("INSERT INTO `images` (`userid`, `title`, `oldfile`, `newfile`, `thumb`) VALUES ('".$session_id."', '".$title."', '".$oldfile."', '".$newfile."', '".$thumb."')");
						include("image_resize.php");
						echo "<p>Upload successful!</p>";
					}else{
						echo "<p>Failure</p>";
					}
				}
			}else{
				echo "<p>Invalid file type!</p>";
			}
		}
	}
	?>
        </td>
    </tr>
</table>
</form>

<?php
function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) { 
$s_path = trim($s_path); 
$o_path = trim($o_path); 
$save = $s_path . $save; 
$file = $o_path . $file; 
$ext = strtolower(end(explode('.',$save))); 
list($width, $height) = getimagesize($file) ; 
if(($width>$t_w) OR ($height>$t_h)) { 
	$r1 = $t_w/$width; 
	$r2 = $t_h/$height; 
	if($r1<$r2) { 
		$size = $t_w/$width; 
	}else{ 
		$size = $t_h/$height; 
	} 
}else{ 
	$size=1; 
} 
$modwidth = $width * $size; 
$modheight = $height * $size; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
switch ($ext) { 
	case 'jpg': 
	case 'jpeg': 
		$image = imagecreatefromjpeg($file) ; 
		break; 
	case 'gif': 
		$image = imagecreatefromgif($file) ; 
		break; 
	case 'png': 
		$image = imagecreatefrompng($file) ; 
		break; 
} 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
imagejpeg($tn, $save, 100) ; 
return; 
}

$file = $newfile;
$filename = $dir . $file;

/* Profile Picture */
$save = $filename;
$t_w = 380;
$t_h = 137;
$o_path = "upload/"; 
$s_path = "upload/newfile/";

/* Thumbnail Picture */
$t_w = 50;
$t_h = 50;
$o_path = "upload/newfile/"; 
$s_path = "upload/thumb/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.