Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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

Posts: 32
Trades: 0
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
 
 
Register now for full access!
Old 04-04-2008, 03:43 AM Re: Improvements in a Image Resize Script
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
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()
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
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
Experienced Talker

Posts: 32
Trades: 0
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
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
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();

__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Improvements in a Image Resize Script
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.13319 seconds with 12 queries