/    Sign up×
Community /Pin to ProfileBookmark

Is there a problem here ?

Hi all,

I was looking at the fnction that I am using for uploading my image files and it looks to me like there is an error in the width calclation – but I am not sure 😮

Here is the function:

[code=html]function makeThumbnail($source, $t_ht,$N_pix_n, $message2=NULL )
{
if(!($o_im = @imageCreateFromString(@file_get_contents($source))))
{
$message2 = “Error: The image supplied was corrupt or an invalid type!”;
return $message2;
}
$t_wd = round($t_ht * ($o_wd = imagesx($o_im)) / ($o_ht = imagesy($o_im))) ;
if(!($N_image = @imagecreatetruecolor($t_wd, $t_ht)))
{
$message2 = “Error: Problem with image dimensions!”;
return $message2;
}
imagecopyresampled($N_image, $o_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd, $o_ht);
$destination = $_SERVER[‘DOCUMENT_ROOT’].”/images/$N_pix_n”;
if(!(@imageJPEG($N_image,$destination,90)))
{
$message2 = “Error: Could not save image!”;
return $message2;
}
imageDestroy($o_im);
imageDestroy($N_image);
} // End of Function [/code]

Now the $t_ht variable is = 225

but the calculation for the width is:
$t_wd = round($t_ht * ($o_wd = imagesx($o_im)) / ($o_ht = imagesy($o_im))) ;

This appears to have two or three equal signs in it ???
That can’t be correct can it ?

What I am trying to do is get a properly proportioned image that is
225px high and 300px wide.

Would much appreciate some maths help 😮

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@jeddikauthorJan 09.2007 — I am thinking of adding this line,

[code=html]if($t_wd > 300){
$t_wd = 300;
}[/code]


I want to insert it directly after the calculation line in order to limit the maximum width of the image.

But what will it do to the image ? Will it crop the image at 300px or will it squash the image to make it fit the 300 limit ?

Thanks for any guidance ?
Copy linkTweet thisAlerts:
@NightShift58Jan 10.2007 — [code=php]<?php
function makeThumbnail($source, $t_ht,$N_pix_n, $message2=NULL ) {
if(!($o_im = @imageCreateFromString(@file_get_contents($source)))) {
$message2 = "Error: The image supplied was corrupt or an invalid type!";
return $message2;
}
// $current_height = imagesy($o_im);
// $current_width = imagesx($o_im);
// $new_height = $t_ht;
// $new_width = $new_height * $current_width / $current_height;
// Therefore : ...
$t_wd = round($t_ht * imagesx($o_im) / imagesy($o_im)) ;

if ($t_wd > 300) {
$t_wd = 300;
$t_ht = round($t_wd * imagesy($o_im) / imagesx($o_im)) ;
}

if(!($N_image = @imagecreatetruecolor($t_wd, $t_ht))) {
$message2 = "Error: Problem with image dimensions!";
return $message2;
}
imagecopyresampled($N_image, $o_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd, $o_ht);
$destination = $_SERVER['DOCUMENT_ROOT']."/images/$N_pix_n";
if(!(@imageJPEG($N_image,$destination,90))) {
$message2 = "Error: Could not save image!";
return $message2;
}
imageDestroy($o_im);
imageDestroy($N_image);
} // End of Function
?>[/code]
The above contains code that may work for you. From your comments, I understood that you were trying to generate a thumbnail of height of $t_ht pixels ... BUT ... if the thumbnail width were to exceed 300px, then resize to 300px wide.

It can be done and I've included it in the post. Just resizing to w=300x would, as you suspected, give the image new proportions.
Copy linkTweet thisAlerts:
@jeddikauthorJan 10.2007 — Thanks - thats what I thought was needed ?
Copy linkTweet thisAlerts:
@NightShift58Jan 10.2007 — You're welcome!
×

Success!

Help @jeddik spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...