Re: Advanced maths help (part 2)
| From: | Chris Dowell | Date: | Fri, 15 Oct 2004 19:30:37 +0000 |
| Subject: | Re: Advanced maths help (part 2) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-199620@lists.php.net to get a copy of this message | ||
I think you've a couple of problems with your second problem (problem problem problem).
First of all, you're setting $dst_h to 0, which gives you a height of 0 - IIRC that's not what you wanted :)
You should be setting that $dst_h = 120 as you always want it to be 120px
I reckon that your code should look more like this:
<?php
// assuming that we're creating two thumbnails, one has h=120, w=?, the second has h=120, w=90
// we'll also assume that the original has h=600, w=800
$src_img = imagecreatefromjpeg("tgp1.jpg");
$src_h = imagesy($src_img); // 600
$src_w = imagesx($src_img); // 800
$dst_h = 120;
$dst_w = $src_w * ($dst_h / $src_h);
$dst_img = imagecreatetruecolor($dst_w,$dst_h);
imagecopyresampled ($dst_img, $srcImage, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
// so far so good, now, lets save the file
imagejpeg($dst_img, 'test.jpg');
unset($dst_img);
// time for problem 2
// first we need a h=120, w=90 image
// image size for h=120, w=90 thumbnail
$dst_h = 120;
$dst_w = 90;
$dst_img = imagecreatetruecolor($dst_w, $dst_h);
// we will be copying to the top left corner of the new thumbnail
$dst_x = 0;
$dst_y = 0;
// now, let's find the top-left offset from the larger original image
$src_x = ($src_h - $dst_h) / 2;
$src_y = ($src_w - $dst_w) / 2;
// no need for imagecopyresampled as we'll be doing 1-1 pixel copying here
imagecopy ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
// so far so good, now, lets save the file
imagejpeg($dst_img, 'test2.jpg');
unset($dst_img);
?>
<img src="test.jpg" /><img src="test2.jpg" />
------------
Hope this helps (although I haven't tested it)
Cheers
Chris
Mag wrote:
Hi again guys, :-) Thanks to the educated (and excellient) help of Chris and trevor I am now getting proportionate thumbs (which was "problem 1", remember? ) Heres the code that i am using, if it may help anyone else: ** code start ** $src_img = imagecreatefromjpeg("tgp1.jpg"); $img_dim = getimagesize("tgp1.jpg"); $h_1 = $img_dim[1]; $w_1 = $img_dim[0]; $dst_h = 120; $dst_w = $w_1 * ($dst_h / $h_1); $dst_img = imagecreatetruecolor($dst_w,$dst_h); $src_w = imagesx($src_img); $src_h = imagesy($src_img); imagecopyresampled($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h); // problem 1 solved till here ** code end ** Then i am trying to work with problem 2, instead of starting all over again from the beginning of getting the images dimensions etc I am continueing and trying to get it straight from the above like so: ** code start problem 2** $dst_w = ($dst_w - 90) / 2; $dst_h = 0; // because the image is only 120px high imagecopyresampled($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h); ImageJPEG($dst_img,'test.jpg'); echo "<img src=test.jpg><br>The bitch works!"; ** code end problem 2 ** When i check the output I am only getting the first parts output... is what i am doing even possible? or do I have to do everything from the start for problem 2 (eg: read from disk, calculate dimensions etc) Thanks, Mag ===== ------ - The faulty interface lies between the chair and the keyboard. - Creativity is great, but plagiarism is faster! - Smile, everyone loves a moron. :-) __________________________________ Do you Yahoo!? Yahoo! Mail Address AutoComplete - You start. We finish. http://promotions.yahoo.com/new_mail