PDA

View Full Version : Force $variable to be numeric or a real number?


student101
04-03-2009, 06:54 AM
I have a varaible $MAXwh = 299;
I am sure this and the others are right, yet the getimagesize doesn't like it, when it's dynamic.
// First attempt...
echo (!is_numeric($MAXwh))?"not a number"."<br />": $MAXwh."<br />";
// Result= 299

// Second attempt...
if(!is_numeric($MAXwh)){
echo "not a number";
}else{
echo $MAXwh;
}
// Result= 299

// Third attempt...
if (is_int($MAXwh)){
echo $MAXwh;
}else{
echo "not a number";
}
// Result= 299

// Last attempt...
if (is_numeric($MAXwh)){
echo $MAXwh;
}else{
echo "not a number";
}
// Result= 299

Anyway to force the $variable to be a number or numeric even though it is? :mrgreen:

edbr
04-03-2009, 07:48 AM
if you hard code $MAXwh, the getimagesize works?

student101
04-03-2009, 07:55 AM
if you hard code $MAXwh, the getimagesize works?
YES if I harcode it in it will also work...
This is my entire make_thumb function...
// ****************
//IF I ADD IT HERE IT WONT WORK.
$MAXwh = 299;
// ****************
function make_thumb($img_name,$filename,$new_w,$new_h){
// ****************
//IF I ADD IT HERE IT WORKS.
$MAXwh = 299;
// ****************
//get image extension.
$ext=getExtension($img_name);
//creates the new image using the appropriate function from gd library
if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
$src_img=imagecreatefromjpeg($img_name);
if(!strcmp("png",$ext))
$src_img=imagecreatefrompng($img_name);
if(!strcmp("gif",$ext))
$src_img=imagecreatefromgif($img_name);
$tmpName = $img_name;
list($width, $height, $type, $attr) = getimagesize($tmpName);
if($width<=$MAXwh || $height<=$MAXwh){
//die("exceeded image dimension limits.");
$old_x=imagesx($src_img);
$old_y=imagesy($src_img);
$thumb_h=$old_y;
$thumb_w=$old_x;
}else{
//gets the dimmensions of the image
$old_x=imagesx($src_img); //width
$old_y=imagesy($src_img); //height
$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;
if($ratio1>$ratio2){
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}else{
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}
}

I guess my question is how to make $MAXwh a GLOBAL $varaible to use any where in the page if added to the top?

student101
04-03-2009, 08:31 AM
Basically 'cause it's a function you can only do things inside the functions specified area between the { and }
Pretty cr** but hey, what can I do?

GOT IT!!!

//Set your &variable...
$MAXwh = 299;
//Do your function thingy...
function bobs uncle(){
//add the global thingy...
global $MAXwh;
//do other stuff...
...
}

student101
04-03-2009, 08:28 PM
I have found a slight fault with this function.
When I want to upload an image of +-460px by +-60px it doesn't actually resize the image to the specified dimensions, i.e. 299 by proportionate size of height or vice versa.

Any ideas on how to get the image resizes to the proper proportions as needed?

student101
04-03-2009, 10:15 PM
I think I figured it out... && instead of ||


if($width<=$MAXwh && $height<=$MAXwh){
//die("exceeded image dimension limits.");
$old_x=imagesx($src_img);
$old_y=imagesy($src_img);
$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;
$thumb_w=$old_x;
$thumb_h=$old_y;
}else{
$old_x=imagesx($src_img); //width
$old_y=imagesy($src_img); //height
$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;
if($ratio1>$ratio2){
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}else{
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}



Scrap that one, this doesn't help me either!

student101
04-04-2009, 08:16 AM
I am hoping that someone can help me put this into perspective?
I seem to be getting lost in the code...

I would like to know the proper ratio information to help resize the image and keep the proportions or aspect ratio.
How does this work or how should it work?
[code]$old_x=imagesx($src_img); //width
$old_y=imagesy($src_img); //height
$ratio1=$old_x/$new_h;
$ratio2=$old_y/$new_w;
if($ratio1>$ratio2){
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}else{
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}


Cheers

davidj
04-04-2009, 08:30 AM
whats the issue again

i got lost in the post!

better to do this using OOP methods

student101
04-04-2009, 08:55 AM
whats the issue again
i got lost in the post!
better to do this using OOP methods
Hi Davidj,

Edit start ...
...THIS IS OOP ORIENTATED...
It's a function that get used later on in the page...
Edit end ...

The issue is the ratio or aspect ratio or dimensions...

I have been searching for days and this is one of those that just doesn't seem to have an answer or at least a descriptive one.

Do you know how the ratio / dimension system is supposed to work?
Why divide $ratio1=$old_x/$new_h; and so on...

Edit start ...
I am sure there is a description somewhere that can explain why or how the resizing with proportion is supposed to work, or is it all trial and error?
Edit end ...

davidj
04-04-2009, 09:09 AM
here is a class which i used before

it might help

uploads image and creates thumbs

browse for the example.php in your PHP environment

student101
04-04-2009, 09:18 AM
Cool thanks,
Does this image "lssll.jpg" on line "35" in "upload_class.php" become dynamic or does it stay?
Is there a reason why you set them to be 600 and 100?

davidj
04-04-2009, 09:25 AM
its just a default

its not used in the process

student101
04-04-2009, 09:36 AM
It's good that I have an idea of what your code does but I still don't understand the ratio parts?
I mean is there a hidden manual where only a few can access to it??

I am not on that level of creating functions with -> in them yet.

... Edit start ...
One thing I really like about this code is that still resizes the thumbnail even though the image size stays the same.
I hope you understand what I mean...
Why divide or multiply the widths and or heights?
... Edit end...

davidj
04-04-2009, 11:10 AM
The class uses a built in function which resamples the original therefore you dont need to perform any calculations to get and set the thumb size.

your at a level where you need to look at Object Orientated Programming

this will help you understand easier

student101
04-04-2009, 11:13 AM
Ok will review the code.
Thank you, I do appreciate the time.

student101
04-07-2009, 07:14 PM
The class uses a built in function which resamples the original therefore you dont need to perform any calculations to get and set the thumb size.your at a level where you need to look at Object Orientated Programming this will help you understand easier
Getting the correct ratios to resize an image...

// -- startdynamic method
//add your image name below...
$img_name = "imagename.jpg"; //Could be dynamic
list($width, $height) = getimagesize($img_name); //get the image width and height
//Display width and height
echo "<strong>Real Width and Height</strong><br />";
$img_width = $width;
$img_height = $height;
echo "img_width = ".$img_width."<br />";
echo "img_height = ".$img_height."<br />";
echo "<hr />";
// -- end dynamic method

//remove comments lines below "/*" if 'NOT' using dynamic method..
/*
//used here as a demo...
$img_width = 800;
$img_height = 600;
*/
//remove comments lines above "*/" if 'NOT' using dynamic method..

echo "img_width = ".$img_width."<br />";
echo "img_height = ".$img_height."<br />";
echo "<hr />";
//-----------
echo "<strong>Set the width and height needed</strong><br />";
$set_width = 250;
$set_height = 250;
echo "set_width = ".$set_width."<br />";
echo "set_height = ".$set_height."<br />";
echo "<hr />";
//-----------
echo "<strong>Work out your ratio needed for a properly resized image</strong><br />";
echo "<br />";
$ratio1 = $set_width / $img_width;
echo "<strong>ratio1 = set_width / img_width = ratio1</strong><br />";
echo "$set_width / $img_width = $ratio1<br />";
echo "ratio1 = $ratio1<br />";
echo "<strong>The NEW height calculation:</strong><br />";
echo "<strong>new_height = img_height * ratio1 = new_height</strong><br />";
$new_height = $img_height * $ratio1;
echo "This height will be: <strong>$new_height</strong><br />";
echo "<hr />";
//-----------
echo "<strong>Work out your WIDTH ratio needed for a properly resized image</strong><br />";
echo "<br />";
$ratio2 = $set_height / $img_height;
echo "<strong>ratio2 = set_height / img_height = ratio2</strong><br />";
echo "$set_height / $img_height = $ratio2<br />";
echo "ratio2 = $ratio2<br />";
echo "<strong>The NEW width calculation:</strong><br />";
echo "<strong>new_width = img_height * ratio2 = new_width</strong><br />";
$new_width = $img_height * $ratio2;
echo "This width will be: <strong>$new_width</strong><br />";
echo "<hr />";It's so much safer / better to understand what you are doing, rather than "copy and paste" - Me

This could be easily turned into a function, just didn't add it...
Cheers