Jump to content

image resize


lional

Recommended Posts

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
https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-630283
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
https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-631649
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
https://forums.phpfreaks.com/topic/122080-image-resize/#findComment-636039
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.