473,394 Members | 2,100 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,394 software developers and data experts.

how and where to insert image resize function

41
hi
i created a simple upload script which works fine and uploads image.

i m not involving validation at present.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($_FILES['photo']['name'])
  3. {
  4.     echo $file_name = $_FILES['photo']['name'];
  5.     echo "<br>";
  6.     echo $file_tmp =$_FILES['photo']['tmp_name'];
  7.  
  8.     move_uploaded_file($_FILES['photo']['tmp_name'], 'upload/'.$_FILES['photo']['name']);
  9. }
  10. ?>
  11. <!DOCTYPE html>
  12. <html>
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  15. <title>Untitled Document</title>
  16. </head>
  17. <body>
  18. <form action="" method="post" enctype="multipart/form-data">
  19.     Your Photo: <input type="file" name="photo" size="25" />
  20.     <input type="submit" name="submit" value="Submit" />
  21. </form>
  22. </body>
  23. </html>
  24.  
now i have this code that resizes the image
Expand|Select|Wrap|Line Numbers
  1. // The file
  2. $filename = 'images/s1.jpg';
  3.  
  4. // Set a maximum height and width
  5. $width = 200;
  6. $height = 200;
  7.  
  8. // Content type
  9. header('Content-Type: image/jpeg');
  10.  
  11. // Get new dimensions
  12. list($width_orig, $height_orig) = getimagesize($filename);
  13.  
  14. $ratio_orig = $width_orig/$height_orig;
  15.  
  16. if ($width/$height > $ratio_orig) {
  17.    $width = $height*$ratio_orig;
  18. } else {
  19.    $height = $width/$ratio_orig;
  20. }
  21.  
  22. // Resample
  23. $image_p = imagecreatetruecolor($width, $height);
  24. $image = imagecreatefromjpeg($filename);
  25. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  26.  
  27. // Output
  28. imagejpeg($image_p, null, 100);
  29.  
so i tried to combine them as they both work fine independently
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($_FILES['photo']['name'])
  3. {
  4.     echo $file_name = $_FILES['photo']['name'];
  5.     echo "<br>";
  6.     echo $file_tmp =$_FILES['photo']['tmp_name'];
  7.  
  8.     //resize function starts
  9.     $filename = 'images/'.$file_name;
  10.  
  11.     // Set a maximum height and width
  12.     $width = 200;
  13.     $height = 200;
  14.  
  15.     // Get new dimensions
  16.     list($width_orig, $height_orig) = getimagesize($filename);
  17.  
  18.     $ratio_orig = $width_orig/$height_orig;
  19.  
  20.     if ($width/$height > $ratio_orig) {
  21.    $width = $height*$ratio_orig;
  22.     } else {
  23.     $height = $width/$ratio_orig;
  24.     }
  25.  
  26.     // Resample
  27.     $image_p = imagecreatetruecolor($width, $height);
  28.     $image = imagecreatefromjpeg($filename);
  29.     imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  30.  
  31.     // Output
  32.     imagejpeg($image_p, null, 100);
  33.     // resize function ends
  34.  
  35.     move_uploaded_file($_FILES['photo']['tmp_name'], 'upload/'.$_FILES['photo']['name']);
  36. }
  37. ?>
  38. <!DOCTYPE html>
  39. <html>
  40. <head>
  41. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  42. <title>Untitled Document</title>
  43. </head>
  44. <body>
  45. <form action="" method="post" enctype="multipart/form-data">
  46.     Your Photo: <input type="file" name="photo" size="25" />
  47.     <input type="submit" name="submit" value="Submit" />
  48. </form>
  49. </body>
  50. </html>
  51.  
so i have two questions related to above combined code

1) The resample or resize function code will come before or after "move_uploaded_file" function ??

2) what will i write in the "move_uploaded_file" function so that resized thumbnail gets uploaded ??

vineet
Sep 15 '16 #1
0 1012

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

Similar topics

0
by: Jay | last post by:
Hi guys, trying to fix a problem with an image resize routine. The code posted below uploads and resizes a jpeg, probablem is, the outlook can look a bit bitty becuase of teh samller size...is...
4
by: vunet.us | last post by:
Hello, What is the best image resizing tool is out there? I want to resize images in ASP page when users upload them into some directory. Your recommendations are super welcome. Thank you.
1
by: atrus123 | last post by:
Aloha all, I'm trying to create a function that will create a thumbnail while preserving the aspect ratio. I had this working, but I made some changes and now the files are no longer being...
12
by: Shawn Northrop | last post by:
Ive been searching for an image resize tutorial for a while now and found this code which worked nicely. I was unable to find the full source code but i think i pieced together the code from the...
2
by: Dominic Vella | last post by:
Hi, I know I seem to have the really complicated questions, but I guess that's why I'm here. This is a little verbose, only because I've been trying to crack this for a week now. Your help...
8
by: infoseekar | last post by:
Image Resize & Rotation Hi I have 2 scripts, one for Image rotation and other image resize and they both are working. Image resize scripts load the picture and resize it and Image rotation...
2
by: Noorain | last post by:
Hi, another problem. i upload width=800 pixels image in database through. this image resize by thumb image & bis image. thumb image width is 100 pixel & big image width is 400 pixel. 800 pixel image...
22
by: simon2x1 | last post by:
i have an image which width is 213 and height is 200 when i echo the image and i resize it echo "<img src='company/$present' width='70' height='68'/>"; the image was not as clear as when it was...
3
by: Nabeel Nazir | last post by:
How can we write resize function for Matrix2 class; where this function need to use Protected data members of matrix1 class? Only setters are available in Matrix 1 class but there is no getter...
1
by: fran7 | last post by:
Hi, I have this asp.net code to resize my images. It works fine although it resizes images without keeping the aspect ratio. My image display page is an .asp page extension. I would rather keep...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.