Reply
Improvements in a Image Resize Script
Old 04-03-2008, 03:25 PM Improvements in a Image Resize Script
XRS
Average Talker

Posts: 25
Hi, I'm making a script to resize images.
I would like to improve it with two more things:
1. Restrict it to JPG and JPEG images printing a message if the extension is not JPG or JPEG;
2. Print a message if file, new width and new height is left blank.

My code is here:
PHP Code:
<?php
// This is the temporary file created by PHP
$uploadedfile $_FILES['uploadfile']['tmp_name'];
$newwidth$_POST['width'];
$newheight$_POST['height'];


//Get File
$file $_FILES['uploadfile'];


// Create an Image from it so we can do the resize
$src imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// This will get the pixels entered by user

$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename "images/"$_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);
echo 
"<center>Resized image to \"$newwidth\"x\"$newheight\"</center>\n";
echo 
"<center><img src=\"$filename\" border='0'></center>\n";
imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
// has completed.
?>
Could somebody help me please?
XRS is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 04-04-2008, 03:43 AM Re: Improvements in a Image Resize Script
mtishetsky's Avatar
Super Spam Talker

Posts: 865
Location: Volendam, Netherlands
1. getimagesize() to ensure it is jpeg by file header, not by extension
2.
PHP Code:
if ($_FILES['file']['error'] > || $_POST['width'] == '' || $_POST['height'] == 0) {
   
error_message();
}
else {
   
continue_processing();

3. Use ImageMagick to convert images instead of imagecopyresampled()
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 04-04-2008, 01:36 PM Re: Improvements in a Image Resize Script
XRS
Average Talker

Posts: 25
Blank fields are resolved.
But only JPG or JPEG I can't do it...
There's something I can't do.
XRS is offline
Reply With Quote
View Public Profile
 
Old 04-07-2008, 01:49 AM Re: Improvements in a Image Resize Script
mtishetsky's Avatar
Super Spam Talker

Posts: 865
Location: Volendam, Netherlands
array getimagesize ( string filename [, array imageinfo])

Returns an array with 4 elements. Index 0 contains the width of the image in pixels. Index 1 contains the height. Index 2 is a flag indicating the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF.

This means that you should do like
PHP Code:
$info getimagesize($_FILES['f']['tmp_name']);
if (
$info[2] != 2) { // is not a jpeg
   
fail();

mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Improvements in a Image Resize Script
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12397 seconds with 13 queries