Jump to content

Another quick question


slpctrl

Recommended Posts

<?php
//begin function

function sizemark($image, $w, $h)
{
   header("Content-type: image/jpeg");
   $image = $_GET['image'];
   $w = $_GET['w'];
   $h = $_GET['h'];
   $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['h'];
if(isset($image) && isset($width) && isset($height) && !empty($image) && !empty($width) && !empty($height))
sizemark($image,$width,$height);
else
die();
?>

 

This script works, but what if I wanted to set the default value of the image to say 320x240? I know in theory it should be easy, but in the function it's looking for the GET values, not anything hard coded from the function so I can't figure out how to make a default. Here was my attempt, but it doesn't work:

 

<?php
//begin function

function sizemark($image, $w, $h)
{
   header("Content-type: image/jpeg");
   $image = $_GET['image'];
   $w = $_GET['w'];
   $h = $_GET['h'];
   $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['h'];
if(isset($image) && isset($width) && isset($height) && !empty($image) && !empty($width) && !empty($height))
sizemark($image,$width,$height);
elseif (isset($image) && empty($width) && empty($height))
sizemark($image,320,240);
else
die();
?>

Link to comment
https://forums.phpfreaks.com/topic/104016-another-quick-question/
Share on other sites

hi there jsut change

  $w = $_GET['w'];

  $h = $_GET['h'];

to

  $w = "320";

  $h = "240";

 

But that would hard code the entire thing. I want the option of giving it any dimension I want, but if a dimension isn't given I want that dimension to be assigned those values.

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.