VB icon

Resize Image in PHP (JPG/JPEG)

Email
Submitted on: 1/2/2015 6:31:00 AM
By: Nathan A. Huebner (from psc cd)  
Level: Advanced
User Rating: By 2 Users
Compatibility: PHP 4.0
Views: 650
 
     Resize an Image from any source. Could be local or remote. You can specify the image quality, height, width, etc. I believe this may only work with JPG/JPEG, I have only tried it with that. If it works with others, than cool! Vote! :)
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Resize Image in PHP (JPG/JPEG)
//**************************************
Free code man!
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Resize Image in PHP (JPG/JPEG)
// Description:Resize an Image from any source. Could be local or remote. You can specify the image quality, height, width, etc. I believe this may only work with JPG/JPEG, I have only tried it with that. If it works with others, than cool! Vote! :)
// By: Nathan A. Huebner (from psc cd)
//
// Returns:Shows before image, and After image, based on code specifications.
//
// Side Effects:May not work with other formats than JPG/JPEG.
//**************************************

<? 
# If you think this script is too complicated, you should see other peoples scripts.
# I saw a script with 40 documents, and over 30,000 lines of code, just to do this.
$myimage="http://i2.microsoft.com/h/all/i/ms_masthead_8x6a_ltr.jpg";
$whereto = "C:\images\micrologo.jpg"; # Make sure this folder exists...
$mywidth=280; # sets the width of the NEW image
$myheight=35; # sets the height of the NEW image
$myquality=100; # sets the Quality of the NEW image (recommend 100 for best image)
echo "Original: $myimage (PSCode made me remove the SRC Code to show you the image, you will have to browse manually to find the image)<br><br>";
ResizeImage($myimage,$whereto,$mywidth,$myheight,$myquality);
echo "New Image: $whereto PSCode made me remove the SRC Code to show you the image, you will have to browse manually to find the image <br><br> <b>(If you see the image, then it saved correctly!)</b><br><br>Don't forget to vote! I had to look everywhere to learn this script.";
function ResizeImage($path_in,$path_out,$new_width,$new_height,$quality) {
# Set Quality to 100, or your images will look blurry.
# Reduce Quality if size is an issue.
# This will create an entirely NEW image, and will not remove the original
# IMPORTANT: I don't know if this works with anything but JPG/JPEG. 
# You may need to convert your images to JPG/JPEG before using this.
		$width = $new_width; 
		$height = $new_height;
$new = ImageCreateTrueColor($width, $height); 
$source = ImageCreateFromJPEG($path_in); 
ImageCopyResampled($new, $source, 0, 0, 0, 0, $width, $height, 
	ImageSX($source), ImageSY($source)); 
ImageJPEG($new, $path_out, $quality); 
# Returns the path of the resized image.
return $path_out;
}
?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.