473,480 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

mysql/php image resize proportionally problem

hey guys - i need help with this code... currenty it just takes an
image and resize it to 450 by 337. What i need it to do is to make the
width 450, and just have the height be resized proptionally to widths
new lenght. Thanks!

if(!$bad_file_flag)
{
// filename will just be the current UNIX timestamp...
$fname = time() . $i;

$new_width = 450;
$new_height = 337;
// move_uploaded_file($_FILES[$element_name][tmp_name],
$vehicle_image_dir . "/$fname$file_extension");
// chmod($vehicle_image_dir . "/$fname$file_extension", 0666);
$tmp_image=imagecreatefromjpeg($_FILES[$element_name]['tmp_name']);
$width = imagesx($tmp_image);
$height = imagesy($tmp_image);
$new_image = imagecreatetruecolor($new_width,$new_height);
ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width,
$new_height, $width, $height);
ImageJPEG($new_image,$vehicle_image_dir .
"/$fname$file_extension");
ImageDestroy($tmp_image);
ImageDestroy($new_image);

// move_uploaded_file($image_buffer, $vehicle_image_dir .
"/resized$fname$file_extension");
chmod($vehicle_image_dir . "/$fname$file_extension", 0666);

mysql_query("INSERT INTO dah_images (filename, vehicle_id)
VALUES('$fname$file_extension', $insert_id)");
}
else
{
// remove the bad file - we don't know what this is!
@unlink($_FILES[$element_name][tmp_name]);
}

Jul 17 '05 #1
8 3409
Get the current height & width of the image you are going to
manipulate.

Imagine it's 750 px wide, and 540 pixels high.

The new picture HAS to be 450 wide. 100% / 750 (original width) * 450
(new width) = 60 %, which means the new image will be 60% smaller.

New height = 540 (old height) / 100 % (gets 1 %) * 60 % (new
percentage) = 324

750 x 540 = 450 x 324.

All just a matter of maths... :D

Jul 17 '05 #2
Yeah i know how to do it mathematically but just have a hard time
making it into code...

Jul 17 '05 #3
hmm, sorry then, i must be missing something here...

Jul 17 '05 #4
i just don't know how to insert the code that you wrote into the code
that i have.... thanxs

Jul 17 '05 #5
First get the image size of the original image.
http://php.net/getimagesize.

Transform that with my maths and then replace the following in your
code:

$new_width = 450;
$new_height = 337;

With:

$new_width = 450;
$new_height =
$the_new_height_i_showed_you;

If still not clear, ask! :D
Good luck with it.

Jul 17 '05 #6
so here is my problem

$new_width = 450;
$new_height = How do i transform the math
into code? i don't know the proper way to do it....

thanxs for the help

Jul 17 '05 #7
I haven't got any time today to help you out, but i'll have a look on
it on monday...

Jul 17 '05 #8
If i'm not mistaking, the following code should be ready to go:

if(!$bad_file_flag)
{

// define new width, and height will be calculated by itself
$new_width = 450;

// filename will just be the current UNIX timestamp...
$fname = time() . $i;

//
move_uploaded_file($_FILES[$element_name][tmp_name],$vehicle_image_dir
.. "/$fname$file_extension");
// chmod($vehicle_image_dir . "/$fname$file_extension", 0666);
$tmp_image =imagecreatefromjpeg($_FILES[$element_name]['tmp_name']);
$width = imagesx($tmp_image);
$height = imagesy($tmp_image);

$new_height = round(($height / 100) * (100 / $width * $new_width),
0);

$new_image = imagecreatetruecolor($new_width, $new_height);
ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width,
$new_height, $width, $height);
ImageJPEG($new_image,$vehicle_image_dir ."/$fname$file_extension");
ImageDestroy($tmp_image);
ImageDestroy($new_image);

// move_uploaded_file($image_buffer, $vehicle_image_dir
.."/resized$fname$file_extension");
chmod($vehicle_image_dir . "/$fname$file_extension", 0666);

mysql_query("INSERT INTO dah_images (filename,
vehicle_id)VALUES('$fname$file_extension', $insert_id)");
}
else
{
// remove the bad file - we don't know what this is!
@unlink($_FILES[$element_name][tmp_name]);
};

Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1412
by: Sam Collett | last post by:
How would you go about create a web control that allows you to upload images and limit the dimensions (width/height) of the image? The control may be put on any page, which may not contain the...
3
1291
by: Neo Geshel | last post by:
Greetings. I am making an admin interface, which allows me to upload photos to an access DB. The admin interface also needs to display the uploaded photos, but only needs to show them at a...
13
2329
by: Neo Geshel | last post by:
I have examined about 80+ different upload scripts on the 'net, both in VB and C#, and none seem to do what I need them to do. Perhaps someone here can point me somewhere that Google hasn't...
6
1169
by: Neo Geshel | last post by:
Greetings. I am making an admin interface, which allows me to upload photos to an access DB. The admin interface also needs to display the uploaded photos, but only needs to show them at a...
2
1739
by: webonomic | last post by:
Converting data types I'm trying to do some image manipulation. This code project article (http://www.codeproject.com/csharp/imageresize.asp) has a great method I want to modefy. Here it is: ...
6
2219
by: evolozik | last post by:
hi everyone i need some help please i am using PHP5 and i used a tutorial and when i copied the code for Resampling an image proportionally, it worked perfectly but when i added it in my page it...
9
4403
by: tshad | last post by:
Is there a way to display images (imageButtons or linkbuttons for instance) as a max size (200px by 50px) and not have it stretch the image? What I want to be able to do is limit the real estate...
6
7041
by: barber.brad | last post by:
We are code crushing the width of images from 285px to 260 pix. However we aren't changing the height. we need a bit of javascript that can make that percentage change to the height as well. Can...
9
1857
by: Tony Sosa | last post by:
Does anyone know what kind of tweaks I need to make to pertinent configs to render an image correctly? Right now, the text of the argument passed to imagejpeg is being shown instead of the image...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7106
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
7022
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
5365
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,...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.