Jump to content

image resize


lional

Recommended Posts

Hi All

I am writing an application where one page uploads an image to my webserver. I would like the next page to display that image but making it smaller to fit on the page but I would still like the image to be in proportion. Is this poissible in PHP

 

Thanks

 

Lional

Link to comment
Share on other sites

Have a look at getimagesize() to get the width and height of an image.

 

To resize an image that is greater then a set width or height you can use this:

 

<?php

list($width, $height) = getimagesize("image.jpg");

$maxWidth = 300;
$maxHeight = 500;

//First check if the image has greater width or height then the maximums
if($width > $maxWidth || $height > $maxHeight){

//If you haven't specified a max width because you are only constraining the height, then we set the widthDivision as the width of the image.
if($maxWidth == ""){
	$widthDivision = $width;
}else{
//If maxWidth is specified, we set the width division as the maxWidth divide by the width of the image.
	$widthDivision = $maxWidth/$width;
}

//Same for height as was said in the width.
if($maxHeight == ""){
	$heightDivision = $height;
}else{
	$heightDivision = $maxHeight/$height;
}

//Now the brains, the min function. Get the lowest number out of the width and height.
$MIN = min($widthDivision, $heightDivision);

//Now we can set the new widths and height by multiplying the lowest number into both the original width and height. This will constrain the proportions...
$newWidth = $MIN*$width;
$newHeight = $MIN*$height;
}else{
//If the image is fine, just set the new width and height as the default.
$newWidth = $width;
$newHeight = $height;
}
?>

Link to comment
Share on other sites

I have tried using this but I get "garbage" printing out instead of my image

Here is my code

$effects_image = $_SESSION['photo'];

  list($width, $height) = getimagesize("$effects_image");

  $maxWidth = 256;

  $maxHeight = 256;

  if ($width > $maxwidth || $height > $maxheight) {

  //If you haven't specified a max width because you are only constraining the height, then we set the widthDivision as the width of the image.

if($maxWidth == ""){

$widthDivision = $width;

}else{

//If maxWidth is specified, we set the width division as the maxWidth divide by the width of the image.

$widthDivision = $maxWidth/$width;

}

 

//Same for height as was said in the width.

if($maxHeight == ""){

$heightDivision = $height;

}else{

$heightDivision = $maxHeight/$height;

}

 

//Now the brains, the min function. Get the lowest number out of the width and height.

$MIN = min($widthDivision, $heightDivision);

 

//Now we can set the new widths and height by multiplying the lowest number into both the original width and height. This will constrain the proportions...

$newWidth = $MIN*$width;

$newHeight = $MIN*$height;

}else{

//If the image is fine, just set the new width and height as the default.

$newWidth = $width;

$newHeight = $height;

 

}

 

 

// Resample

$image_p = imagecreatetruecolor($newWidth, $newHeight);

$image = imagecreatefromjpeg($effects_image);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

 

// Output

imagejpeg($image_p, null, 100);

Link to comment
Share on other sites

Did you get it sorted?

 

 

<?php
///your image generation code ending with(change the file name to the file name and path you want):
imagejpeg($image_p, savepath/image_name.jpg, 100);
echo '<img src="savepath/image_name.jpg" />';

//that create the image tag and you needn't worry about the headers, sessions, output bufferring etc.
?>

 

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.