Jump to content

percents


slpctrl

Recommended Posts

<?php

/*image resizer and watermarker written by slpctrl. to use, simply place on
server along with a watermark.png of your choice, and in the URL (resizer.php or
whatever), you simply append the image location, and optionally you may append
the width and height of the image. If nothing specified, it will make the default
value 320x240. You can use any of the following:

Specify a w and h in the URL as a GET variable
specify the percentage by which the image is decreased. 1=100%, .5=50% and so forth
Specify none, and leave it at it's default values(320x240)
*/

//begin function
function sizemark($image, $w = 320, $h = 240, $percent = 1)
{
   header("Content-type: image/jpeg");
   $percent = $_GET['percent'];
   if(isset($percent) && !empty($percent))
   {
    $w = imagesx($image) * $percent;
    $h = imagesy($image) * $percent;
   }
   $x = @getimagesize($image);
   $sw = $x[0];
   $sh = $x[1];
   $new = @ImageCreateFromJPEG($image) or
   $new = @ImageCreateFromPNG($image) or
   $new = @ImageCreateFromGIF($image) or
   $new = false;
   if(!$new)
   {
      readfile($new);
   }
   else
   {
   $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_y = (imagesy($image) - $watermark_height)/2;
   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

$image = $_GET['image'];
$width = $_GET['w'];
$height = $_GET['height'];
if(isset($image) && !empty($image))
sizemark($image, $width, $height);
else
die();
?>

 

function sizemark($image, $w = 320, $h = 240, $percent = 1)
{
   header("Content-type: image/jpeg");
   $percent = $_GET['percent'];
   if(isset($percent) && !empty($percent))
   {
    $w = imagesx($image) * $percent;
    $h = imagesy($image) * $percent;
   }

 

This is what I'm having troubles with. Here's what I'm trying to do: give the option to specify width and height or a percentage to scale the image down. However, what I've got now isn't doing that. Basically my theory was if the GET variable of percent was set, to calculate it's percentage into the width and height instead of either manually inputting them or using default values. Can anyone help me out?

Link to comment
https://forums.phpfreaks.com/topic/104034-percents/
Share on other sites

Here's my new script:

 

<?php
//begin function
function sizemark($image, $percent = NULL, $w = 320, $h = 240)
{
   header("Content-type: image/jpeg");
   if(isset($percent) && !empty($percent))
   {
    $w = (imagesx($image)*$percent);
    $h = (imagesy($image)*$percent);
   }
   $x = @getimagesize($image);
   $sw = $x[0];
   $sh = $x[1];
   $new = @ImageCreateFromJPEG($image) or
   $new = @ImageCreateFromPNG($image) or
   $new = @ImageCreateFromGIF($image) or
   $new = false;
   if(!$new)
   {
    readfile($new);
   }
   else
   {
   $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_y = (imagesy($image) - $watermark_height)/2;
   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

$image = $_GET['image'];
$w = $_GET['w'];
$h = $_GET['h'];
$percent = $_GET['percent'];
if(isset($image) && isset($w) && isset($h) && !empty($image)
&& !empty($w) && !empty($h))
sizemark($image,,$width,$height);
elseif(isset($percent) && isset($image) && !empty($percent) && !empty($image)
&& empty($w) && empty($h))
sizemark($image,$percent);
elseif(isset($image) && empty($w) && empty($h) && empty($percent))
sizemark($image);
else
die();
?>

 

What I tried to do here was, set the default value of percent to null. Then, inside the function if isset($percent) and not empty, etc etc, to use the percent formula to calculate the width and height, and if only the image and the dimensions are specified, use those and if only the image has been specified then use default values. However, I can't get the percent thing to work for the damn life of me. And by the way, can I use isset within a function to see if a parameter had been entered?

Link to comment
https://forums.phpfreaks.com/topic/104034-percents/#findComment-532571
Share on other sites

If noone can help me there, what about where I used isset to see if a function parameter was set?

 

function sizemark($image, $w = 320, $h = 240, $percent = NULL)
{
   header("Content-type: image/jpeg");
   if(isset($percent))
   {
    $w = imagesx($image) * $percent;
    $h = imagesy($image) * $percent;
   }

Link to comment
https://forums.phpfreaks.com/topic/104034-percents/#findComment-532580
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.