473,396 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Image resizing problem

11
This is in reference to my previos post but a completely different problem.

Previous problem and code can be found here:
http://www.thescripts.com/forum/thread563724.html

When I run the function like this, it gives me weird images (ratio is way off)

[php]
$uploaddir = '/home/avonprom/www/product_images/';
$thumbdir = '/home/avonprom/www/product_images/';
$uploadfile = $uploaddir . $_POST['sku'] . ".jpg";
$thumbfile = $thumbdir . "sm_" . $_POST['sku'] . ".jpg";
$mainfile = $thumbdir . "mid_" . $_POST['sku'] . ".jpg";

if (move_uploaded_file($_FILES['thumbnail']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";

createthumb($uploadfile,$thumbfile,120,100);

createthumb($uploadfile,$mainfile,285,407);

} else {
echo "Possible file upload attack!\n";
}

[/php]

For example, one image was 285 x 450 which should have created a slightly smaller image due to 450 being too wide. Instead it came out 285 x 600 or something like that.

Does anyone see the error in calculation?
Thanks in advance

Aaron

EDIT:
In case you want to see the function here, here it is:

[php]
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
if ($new_w < $new_h)
{
$thumb_w=$new_w;
$thumb_h=$new_w;
}
elseif ($new_h < $new_w)
{
$thumb_w=$new_h;
$thumb_h=$new_h;
}
else
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thum b_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
[/php]
Nov 14 '06 #1
7 2114
aaronic
11
I think this has something to do with the fact that the script was made to think that the width x height ratio would be the same between the source and the end result. The script needs some modification which I am going to look into but I would appreciate if anyone here could figure it out.
Nov 14 '06 #2
ronverdonk
4,258 Expert 4TB
In order to avoid stretching, either height or width, you'll need to crop the image at some point.
Expand|Select|Wrap|Line Numbers
  1. Assume: image 400 x 200.
  2. Required sizes: 200 x 100  = no problem ratio unchanged
  3.                 200 x 50   = ratio 200 x 100: you'd have to crop 50 pixels from one side or 25 from left and right side
  4.                 200 x 150  = ratio 300 x 150: you'd have to crop 100 from the top or 50 from top and bottom
  5.  
You can use imagecopy to crop your image.

Ronald :cool:
Nov 14 '06 #3
aaronic
11
In order to avoid stretching, either height or width, you'll need to crop the image at some point.
Expand|Select|Wrap|Line Numbers
  1. Assume: image 400 x 200.
  2. Required sizes: 200 x 100  = no problem ratio unchanged
  3.                 200 x 50   = ratio 200 x 100: you'd have to crop 50 pixels from one side or 25 from left and right side
  4.                 200 x 150  = ratio 300 x 150: you'd have to crop 100 from the top or 50 from top and bottom
  5.  
You can use imagecopy to crop your image.

Ronald :cool:

Cropping is not an option. I would simply reduce the image size in that situation. I'll use your example and hopefully you can help come up with some code:

Expand|Select|Wrap|Line Numbers
  1. Assume: image 400 x 200.
  2. Required sizes: 200 x 100  = no problem ratio unchanged
  3.                 200 x 50 (final max size)   = final image 100 x 50: Keeping within our 50 height, our image would now be 100x 50
  4.                 200 x 150 (final max size)  = final image 300 x 150: To keep within our max heights of 150.
  5.  
Please help :D
Nov 14 '06 #4
ronverdonk
4,258 Expert 4TB
Ok, so no cropping. But what is the leading size for your thumb, height or width. You'll have to establish that first as the basis for determining the ratio.

Ronald :cool:
Nov 14 '06 #5
aaronic
11
Ok, so no cropping. But what is the leading size for your thumb, height or width. You'll have to establish that first as the basis for determining the ratio.

Ronald :cool:

Thanks for the help, but the script should work no matter what the source image ratio is, or what the thumbnail ratio is. The function allows you to send it any width/height.

I think I am going to just make like 10 different examples and come up with a formula that produces the right answer everytime.

If you have any further help please post :D
Nov 15 '06 #6
aaronic
11
Ok please help :)

I need to create two images, one that is max 120x100 and another 285x407.

Now with this information I guess the function wouldn't have to work with ANY possible destination size.

Does that help?
Nov 15 '06 #7
aaronic
11
RESOLVED!

Changes to code posted below:

[php]
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
//my edits STAR HERE
$old_ratio = $old_x / $old_y;

$new_ratio = $new_w / $new_h;

if ($old_ratio < $new_ratio)
{

$thumb_h=$new_h;
$thumb_w=$new_h * $old_ratio;
}
if ($old_ratio > $new_ratio)
{
$thumb_h=$new_w / $old_ratio;
$thumb_w=$new_w;
}
if ($old_ratio == $new_ratio)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
// AND END HERE. ENJOY
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thum b_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}



[/php]
Nov 15 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Neil Woodvine | last post by:
***Scenario ... I have a DataList with a hyperlink WebControl in the Item Template. I want to display a 64x64 image in the Hyperlink and set the NavigateURL to the full size image. ***Source...
14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
0
by: E | last post by:
I wrote code which Resizes images and then saves them below is the 2 methods. The problem is that half of the resized image turns out gray, as if it wasn't completed. Could someone please tell me...
0
by: E | last post by:
I wrote code which Resizes images and then saves them below is the 2 methods. The problem is that half of the resized image turns out gray, as if it wasn't completed. Could someone please tell me...
3
by: Zahid Khan | last post by:
I need little help in my situation. I am reading a graphic file (jpg) from disk and then resizing it and save resized image. What happens, it gets blured, I want to retain same quality so that...
4
by: James A Taber | last post by:
Problem resizing image.(JPG) If i try to resize an img with horisontal=150 and vertical resolution=150 The quality of the target image is dramatically reduced. Source code is provided below. ...
10
by: David W. Simmonds | last post by:
I have a DataList control that has an Image control in the ItemTemplate. I would like to resize the image that goes into that control. I have a series of jpg files that are full size, full...
8
by: berkshire | last post by:
Hi, Anyone know of a script out there that can resize images and not sacrifice image quality? I've been using phpthumb (http://phpthumb.sourceforge.net/) but when compared to an image resized...
10
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for...
11
by: shapper | last post by:
Hello, I am displaying an image on a few pages. The image size is 50 px height and 50 px width. In some pages I need the image to be 30x30 px in others 40x40 px and in others 50x50px. Can I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.