Jump to content

[SOLVED] Constrain Proportions


jaikob

Recommended Posts

I have a nifty question for all of you.

 

I have a php resize image script, set to size an image to a "certain Size"

 

What I would like to do is this:

 

The width would be set to resize to 103 statically in code, but I would like it to constrain proportions with the height, this would help prevent the image from getting distorted, instead of setting a static width and height, I'd like to set the width, and constrain proportions with the height. How would this be achieved?

 

Here is my code:

 

<?php
if(isset($_POST['submit']))
{		
$path_thumbs = "../images/attorney_photos/";		
$img_thumb_width = 100;

$extlimit = "yes"; 
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");		
$file_type = $_FILES['add_photo']['type'];
$file_name = $_FILES['add_photo']['name'];
$file_size = $_FILES['add_photo']['size'];
$file_tmp = $_FILES['add_photo']['tmp_name'];
if(!is_uploaded_file($file_tmp)){
echo "<span style=\"font-family: Arial, Helvetica, sans-serif; font-size:12px;\">You Have Not Selected A File. <a href=\"$_SERVER[php_SELF]\">Return To Form</a> </span>";
exit();
}
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "<span style=\"font-family: Arial, Helvetica, sans-serif; font-size:12px;\"> That File Extension Type is Not Supported, Please Convert the File to a JPEG, PNG, GIF, or BMP File.</span>";
exit();
}
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
$rand_name = md5(time());
$rand_name= rand(0,999999999);
$ThumbWidth = $img_thumb_width;
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
list($width, $height) = getimagesize($file_tmp);
//
// Set the sizes
$newwidth = 103;
$newheight = 131;
//
//
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($resized_img,"$path_thumbs/$rand_name.$file_ext");
imagedestroy($resized_img);
imagedestroy($new_img);
}
move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
$msg = "$rand_name.$file_ext";
header("Location: attorney_resize.php?msg=$msg");
exit();} ?>

Link to comment
https://forums.phpfreaks.com/topic/122962-solved-constrain-proportions/
Share on other sites

You can take a look at my script. It's a watermarker and resizer. Look towards the bottom

 

 

<?php

/*image resizer and watermarker written by slpctrl.

1. Specify the image, width and height using w and h as GET variables
2. Specify the image alone and let it scale it at it's default 320x240
3. Specify image and percent by which to scale it
4. Specify either w or h and let the script calculate the other dimension

supports JPEG PNG and GIF images

Use: Place in web server with watermark of your choice (watermark.png) in the
same directory. Then you append all the other variables in the URL bar with GET
variables. A list of them are:

w = width
h = height
image = image
p = percent (.25 is 25 percent, 1 is 100 and so forth)
*/

//begin function
function sizemark($image, $w = 320, $h = 240)
{
   header("Content-type: image/jpeg");
   //retrieving the dimensions of image
   $x = @getimagesize($image);
   $sw = $x[0];
   $sh = $x[1];
   //setting the new drawn image as a $new variable based on it's file type
   $new = @ImageCreateFromJPEG($image) or
   $new = @ImageCreateFromPNG($image) or
   $new = @ImageCreateFromGIF($image) or
   $new = false;
   if(!$new)
   {
    readfile($new);
   }
   else
   {
   //Here are the GD library functions in use where we take the image, rescale,
   //get coordinates for watermark and all the other nice details and merge the 
   //watermark in the center of the scaled image and return the new image
   $thumb = @ImageCreateTrueColor($w, $h);
   @ImageCopyResampled($thumb, $new, 0, 0, 0, 0, $w, $h, $sw, $sh);
   $image = $thumb;
   $watermark = imagecreatefrompng('watermark.png');
   $watermark_width = $sw/2;
   $watermark_height = $sh/2;
   $dest_x = (imagesx($image) - $watermark_width)/2;
   $dest_y = (imagesy($image) - $watermark_height)/2;
   imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width,
   $watermark_height, 25);
   imagejpeg($image);
   imagedestroy($image);
   imagedestroy($watermark);
   return $image;
   }
   return false;
}
//end function
//variable setting
$image = $_GET['image'];
$w = $_GET['w'];
$h = $_GET['h'];
$p = $_GET['p'];
//the most important variable-$image must be set in order to do anything.
//this will check to make sure it's set, and if so it will get the dimensions
//of the image, and calculate the percent of scale by multiplying the value
//of each axis and multiply it by the given percentage
if(isset($image))
{
$x = @getimagesize($image);
$sw = $x[0] * $p;
$sh = $x[1] * $p;
}
else
die();
//check to see if image and percent are set and calculate the new dimensions
//based on the percentage given
if(isset($p) && empty($w) && empty($h))
   sizemark($image,$sw,$sh);
//check to see if image and dimensions of thumbnail are to be set
elseif(isset($w) && isset($h))
   sizemark($image,$w,$h);
//check to see if only image and width are set, calculates height
elseif(isset($w) && empty($h))
{
   $h1 = ($w/$x[0]) * $x[1];
   sizemark($image,$w,$h1);
}
//check to see if only image and height are set, calculates width
elseif(isset($h) && empty($w))
{
   $w1 = $x[0] * ($h/$x[1]);
   sizemark($image,$w1,$h);
}
else
sizemark($image);
?>

 

Specifically here:

 

<?php
$x = @getimagesize($image);
$sw = $x[0] * $p;
$sh = $x[1] * $p;
}
else
die();
//check to see if image and percent are set and calculate the new dimensions
//based on the percentage given
if(isset($p) && empty($w) && empty($h))
   sizemark($image,$sw,$sh);
//check to see if image and dimensions of thumbnail are to be set
elseif(isset($w) && isset($h))
   sizemark($image,$w,$h);
//check to see if only image and width are set, calculates height
elseif(isset($w) && empty($h))
{
   $h1 = ($w/$x[0]) * $x[1];
   sizemark($image,$w,$h1);
}
//check to see if only image and height are set, calculates width
elseif(isset($h) && empty($w))
{
   $w1 = $x[0] * ($h/$x[1]);
   sizemark($image,$w1,$h);
}
else
sizemark($image);
?>

 

It lets you specify any dimension in any combination, single dimension on one side, both dimensions and constrains proportions from there. Lemme know if it helps you :D.

 

Edit: Oops posted the wrong one. :D

Imma post this one too. I think they're slightly different I don't know but imma post it anyways :P

 

 

<?php
/*image resizer and watermarker written by slpctrl.

1. Specify the image, width and height using w and h as GET variables
2. Specify the image alone and let it scale it at it's default 320x240
3. Specify image and percent by which to scale it
4. Specify either w or h and let the script calculate the other dimension

supports JPEG PNG and GIF images

Use: Place in web server with watermark of your choice (watermark.png) in the
same directory. Then you append all the other variables in the URL bar with GET
variables. A list of them are:

w = width
h = height
image = image
p = percent (.25 is 25 percent, 1 is 100 and so forth)
*/

//begin function
function sizemark($image, $w, $h)
{
   header("Content-type: image/jpeg");
   //retrieving the dimensions of image
   $x = @getimagesize($image);
   $sw = $x[0];
   $sh = $x[1];
   //setting the new drawn image as a $new variable based on it's file type
   $new = @ImageCreateFromJPEG($image) or
   $new = @ImageCreateFromPNG($image) or
   $new = @ImageCreateFromGIF($image) or
   $new = false;
   if(!$new)
   {
    readfile($new);
   }
   else
   {
   //Here are the GD library functions in use where we take the image, rescale,
   //get coordinates for watermark and all the other nice details and merge the 
   //watermark in the center of the scaled image and return the new image
   $thumb = @ImageCreateTrueColor($w, $h);
   @ImageCopyResampled($thumb, $new, 0, 0, 0, 0, $w, $h, $sw, $sh);
   $image = $thumb;
   $watermark = imagecreatefrompng('watermark.png');
   $watermark_width = imagesx($watermark);
   $watermark_height = imagesy($watermark);
   $dest_x = (imagesx($image) - $watermark_width)/2;
//$dest_x = (imagesx($image) - $watermark_width) - 5;
//The above line, uncommented will put the watermark in the bottom corner
   $dest_y = (imagesy($image) - $watermark_height)/2;
//$dest_y = (imagesy($image) - $watermark_height) - 5;
//same as above
   imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width,
   $watermark_height, 30);
   imagejpeg($image);
   imagedestroy($image);
   imagedestroy($watermark);
   return $image;
   }
   return false;
}
//end function
//variable setting
$image = $_GET['image'];
$w = $_GET['w'];
$h = $_GET['h'];
$p = $_GET['p'];
$x = @getimagesize($image);
$sw = $x[0] * $p;
$sh = $x[1] * $p;
//check to see if image and percent are set and calculate the new dimensions
//based on the percentage given
if(isset($p) && empty($w) && empty($h))
   sizemark($image,$sw,$sh);
//check to see if image and dimensions of thumbnail are to be set
elseif(isset($w) && isset($h))
   sizemark($image,$w,$h); 
//check to see if only image and width are set, calculates height
elseif(isset($w) && empty($h))
{
   $h1 = ($w/$x[0]) * $x[1];
   sizemark($image,$w,$h1);
}
//check to see if only image and height are set, calculates width
elseif(isset($h) && empty($w))
{
   $w1 = $x[0] * ($h/$x[1]);
   sizemark($image,$w1,$h);
}
elseif(empty($w) && empty($h) && empty($p))
sizemark($image,$x[0]*.5,$x[1]*.5)
?>

 

It's heavily commented, just upload it to your server and use it to see how it works. The variables are like so:

 

resizer.php?image=http://www.multimedia-technologies.com/images/portfolio/phpfreaks.jpg&h=500

 

This will set the height to 500 and constrain the other proportion. You could do:

 

resizer.php?image=http://www.multimedia-technologies.com/images/portfolio/phpfreaks.jpg&w=250

 

And the height will be set to constrain it's proportions. It also allows you to scale it by percentage :P.

okay guys, with added solutions and some research I've made a working script, but the picture quality is just horendous.

for  example: This was a 750x1125 image, resized to width: 103 x (proportioned), it sucks!

 

225024751.jpg

 

I checked the proportions of the image via photoshop, and the width and height is correct. It looks grainy? My script is not fairly different, yours just has a lot of nifty resizing =P, but infact I need the script as the one I have do to site complications.

Heres is my modified code:

 

<?php
if(isset($_POST['submit']))
{		
$path_thumbs = "../images/attorney_photos/";		
$img_thumb_width = 100;

$extlimit = "yes"; 
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");		
$file_type = $_FILES['add_photo']['type'];
$file_name = $_FILES['add_photo']['name'];
$file_size = $_FILES['add_photo']['size'];
$file_tmp = $_FILES['add_photo']['tmp_name'];
if(!is_uploaded_file($file_tmp)){
echo "<span style=\"font-family: Arial, Helvetica, sans-serif; font-size:12px;\">You Have Not Selected A File. <a href=\"$_SERVER[php_SELF]\">Return To Form</a> </span>";
exit();
}
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "<span style=\"font-family: Arial, Helvetica, sans-serif; font-size:12px;\"> That File Extension Type is Not Supported, Please Convert the File to a JPEG, PNG, GIF, or BMP File.</span>";
exit();
}
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
$rand_name = md5(time());
$rand_name= rand(0,999999999);
$ThumbWidth = $img_thumb_width;
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
list($width, $height) = getimagesize($file_tmp);
$res_prop_height = imagesy($file_tmp); //Height
$res_prop_width = imagesx($file_tmp); //Width
$newwidth = 103;
$newheight = $height * ($newwidth/$width); 
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($resized_img,"$path_thumbs/$rand_name.$file_ext");
imagedestroy($resized_img);
imagedestroy($new_img);
}
move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
$msg = "$rand_name.$file_ext";
header("Location: attorney_resize.php?msg=$msg");
exit();} ?>

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.