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
Old 03-30-2008, 06:34 PM Image Resizer.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Hello,

How would i go about having a a script which resizes a image so its under 80 X 80 i want this for resizing images which have been uploaded for Avatars.

How can i do this and where can i get a script

Thanksm
Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 03-30-2008, 08:36 PM Re: Image Resizer.
Junior Talker

Posts: 1
Trades: 0
Here's a script I use which you are welcome to try. I adapted it from somewhere else. It uploads a file then creates a thumbnail called tb_filename. Stores both images in a folder you specify.

uploadimage.html
Code:
<html>
<head>
<title>Upload and Resize</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="upload.php">
<input type="file" name="userfile" />
<input type="submit" value="Upload Image" />
</form>
</body>
</html>
upload.php
Code:
<?php

// Get details of uploaded file
$filetmp = $_FILES['userfile']['tmp_name'];
$filename = $_FILES['userfile']['name'];
$length=strlen($filename);

// Limit filename to 40 chars
if ($length>40)
{
print "<p>The file ($filename) cannot be used.  Try again with a shorter filename.</p>\n";
exit;
}

// Directory where uploaded images and thumbnails will be stored
$path = "images/";
$path_chunks = explode("/", $filename);
$thefile = $path_chunks[count($path_chunks) - 1];
$dotpos = strrpos($thefile, ".");
$extension = substr($thefile, $dotpos + 1);

// If no file was uploaded, redirect back to upload page
if (!$filename)
{
header("Location: uploadimage.html");
exit;
}

// Error message if not jpg or JPG
$permitted = array("jpg", "JPG", "jpeg", "JPEG");
if (!in_array($extension, $permitted))
{
print "<p>The file ($filename) cannot be used.  Try again with a jpg/JPG/jpeg/JPEG.</p>\n";
exit;
}

// Save the uploaded file to the directory
$newfile = $path . $filename;
move_uploaded_file($filetmp, $newfile);

// Specify filename for the thumbnail (tb_filename)
$saveto = $path . "tb_" . $filename;

// Get dimensions
list($width, $height) = getimagesize($newfile);

// This resizes a thumbnail to 150px wide (can also do $height/150 to get 150px tall)
$scale = $width / 150;
$new_width = $width / $scale;
$new_height = $height / $scale;

// Create the thumbnail
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($newfile);

// Save the thumbnail
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, "{$saveto}", 100);

// All done - redirect to upload page for next one
header("Location: uploadimage.html");
?>
It only lets you specify a maximum height or width (not both) so if you want 80x80 you may have to fiddle with it. Don't have time to do it now but you could probably work it our yourself.

Make sure you have created a directory to store the images, and that your script has permission to write to it.
murfituk is offline
Reply With Quote
View Public Profile
 
Old 03-31-2008, 12:48 AM Re: Image Resizer.
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
This question appears here once every two weeks. And every time somebody advises to resize images with ImageCopyResampled() which requries a lot of work and later the script start failing with "out of memory" error, because some idiot uploads his 7Mpx photo to make a 80x80 avatar. Use command-line ImageMagick utility called "convert" instead:
PHP Code:
exec("convert {$infile} -scale '80x80>' {$outfile}"); 
Advantages:
- no need to find out the format (jpg, gif, png or whatever) of the input file
- no need to manually set the format of the output file (both are set by utility according to file extension)
- no need to calculate ratio and dimensions of the output file
- no limit on the input file filesize and resolution
- works ten times faster than the script
Disadvantages:
- can only be run through exec() or similar functions which most web developers are afraid of for some unknown reason.
__________________

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 03-31-2008, 07:53 AM Re: Image Resizer.
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
I wil definatly have to try that mtishetsky looks like exactly what i want.

Thanks,
Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to Image Resizer.
 

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