slpctrl Posted May 4, 2008 Share Posted May 4, 2008 What I want to do, is have height width and percent optional. I got the height and width, but I can't get percent to work. My workaround was to set the default value of percent to null, and check if it's not null if it's not, calculate the width and height using my formula but it won't work at all. Can anyone help <?php //begin function function sizemark($image, $w = 320, $h = 240, $percent = NULL) { header("Content-type: image/jpeg"); if (!is_null($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, NULL, NULL, $percent); elseif(isset($image) && empty($w) && empty($h) && empty($percent)) sizemark($image); else die(); ?> Link to comment https://forums.phpfreaks.com/topic/104077-solved-optional-function-parameters/ Share on other sites More sharing options...
slpctrl Posted May 4, 2008 Author Share Posted May 4, 2008 <?php //begin function function sizemark($image, $w = 320, $h = 240) { header("Content-type: image/jpeg"); $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']; if(isset($image) && isset($w) && isset($h) && !empty($image) && !empty($w) && !empty($h)) sizemark($image,$w,$h); elseif(isset($image) && empty($w) && empty($h)) sizemark($image); else die(); ?> How about this. This is my working script, how do I add an optional percent to scale option to it? Link to comment https://forums.phpfreaks.com/topic/104077-solved-optional-function-parameters/#findComment-532850 Share on other sites More sharing options...
ablueycolor Posted May 4, 2008 Share Posted May 4, 2008 I didnt take the time to verify this code works but it is a minor improvement to your original. Please make note of the use of ternary operators at the bottom of the code: <?php //begin function function sizemark($image, $w = 320, $h = 240, $percent = NULL) { header("Content-type: image/jpeg"); if (!is_null($percent)) { $w = imagesx($image) * $percent; $h = imagesy($image) * $percent; } else { $x = @getimagesize($image); $w = $x[0]; $h = $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']; if (!empty($image['name'])) { $w = (!empty($_GET['w'])) ? $_GET['w'] : 320; $h = (!empty($_GET['h'])) ? $_GET['h'] : 240; $percent = (!empty($_GET['percent'])) ? $_GET['percent'] : NULL; sizemark($image, $width, $height); } else die(); ?> Link to comment https://forums.phpfreaks.com/topic/104077-solved-optional-function-parameters/#findComment-532930 Share on other sites More sharing options...
rarebit Posted May 4, 2008 Share Posted May 4, 2008 Well I was just going to swap it about a bit... function sizemark($image, $w = 320, $h = 240, $percent = NULL) to function sizemark($image, $percent = NULL, $w = 320, $h = 240) And call it in these two ways: sizemark($img, NULL, 64, 128); sizemark($img, 50); Your issue was, that you were passing NULL instead of a number, and therefore changing the default value... Link to comment https://forums.phpfreaks.com/topic/104077-solved-optional-function-parameters/#findComment-532934 Share on other sites More sharing options...
ablueycolor Posted May 4, 2008 Share Posted May 4, 2008 Also, I didn't change the line: @ImageCopyResampled($thumb, $new, 0, 0, 0, 0, $w, $h, $sw, $sh); Which still uses the undefined variables $sh and $sw. To continue using these variables, modify the following lines: <?php if (!is_null($percent)) { $sw = imagesx($image); $sh = imagesy($image); $w = $sw * $percent; $h = $sh * $percent; } else { $x = @getimagesize($image); $w = $x[0]; $h = $x[1]; } ?> Link to comment https://forums.phpfreaks.com/topic/104077-solved-optional-function-parameters/#findComment-532938 Share on other sites More sharing options...
slpctrl Posted May 4, 2008 Author Share Posted May 4, 2008 Here's what my new code looks like, percent still doesn't work. <?php //begin function function sizemark($image, $w = 320, $h = 240) { header("Content-type: image/jpeg"); $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']; $p = $_GET['pct']; if(isset($image) && isset($p)) { $a = imagesx($image); $b = imagesy($image); $nw = $a*$p; $nh = $b*$p; sizemark($image, $nw, $nh); } elseif(isset($image) && isset($w) && isset($h)) sizemark($image,$w,$h); elseif(isset($image)) sizemark($image); else die(); ?> This time I decided to search for the GET variable P and if there, calculate the dimensions outside of the function. But I just can't do it :'( Link to comment https://forums.phpfreaks.com/topic/104077-solved-optional-function-parameters/#findComment-532957 Share on other sites More sharing options...
slpctrl Posted May 4, 2008 Author Share Posted May 4, 2008 Ah I figured it out after looking closer into some functions (thought they returned something that they didn't). Here's the workign code and sorry for the hassle <?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) { 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_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 //variable setting $image = $_GET['image']; $w = $_GET['w']; $h = $_GET['h']; $p = $_GET['p']; //check to see if image and percent are set and calculate the new dimensions //based on the percentage given if(isset($image) && isset($p)) { $x = @getimagesize($image); $sw = $x[0] * $p; $sh = $x[1] * $p; sizemark($image,$sw,$sh); } //check to see if image and dimensions of thumbnail are to be set elseif(isset($image) && isset($w) && isset($h)) sizemark($image,$w,$h); //check to see if only image is set, which will use default values elseif(isset($image)) sizemark($image); else die(); ?> Link to comment https://forums.phpfreaks.com/topic/104077-solved-optional-function-parameters/#findComment-532979 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.