PDA

View Full Version : [php] Upload, crop and resize an image.



pumpkinSoup
September 24th, 2006, 12:06 PM
Hello.

I'm new to the forums, and I know a tiny amount of php :(... so I was hoping maybe someone here could help me. :D

Well I'm making a site for a friend of mine, but he is a big techno-fobe. The site is basicly a big image gallery, but he doesn't want to have to crop and resize all of his images to make a thumbnail, so Im tring to create a script that will do that for him (just automatically grap a square from the middle of the picture, about 160*160, then shrink it down to 80*80, and finally just save the file).

But unfortunately (as stated above), I know practically no php :( If you guys could help me out I would really appreciate it :D

mlk
September 24th, 2006, 05:21 PM
I've been trying to do this for a while; whilst the cropping resizing & saving of the image is easily done, the client side is a tad more complicated but feasible: http://www.speedingrhino.com/Image_Cropper_v1.3/cropper_example.htm

pumpkinSoup
September 24th, 2006, 05:48 PM
Nice link.

Although thats alot more complex than what I am trying to create... All I want is to grab a section out of the middle, crop and resize it. The user won't need to do anything but upload the file...

pumpkinSoup
September 25th, 2006, 03:08 PM
I found this tutorial
http://www.tutorialized.com/tutorial/PHP-Thumbnail-Generation-Script/10084

Its almost perfect, exept that it doesn't have an upload feature... How would I combine that with a php upload function in one page?

Thanks

pumpkinSoup
September 26th, 2006, 02:33 PM
In this tutorial

http://www.kirupa.com/developer/mx2004/external_array.htm

How would you adapt it to allow you to list a list of folders instead of files?

Thanks.

raz
September 26th, 2006, 06:01 PM
<form name="upload" method="post" action="script.php">
File:
<input type="file" name="image"/>
</form>




$image = $_POST['image'];
$newWidth = 80; // or 160 not sure.
$newHeight = 80; // or 160 not sure.


As for printing the folders check out these links:
http://us3.php.net/opendir
http://us3.php.net/manual/en/function.readdir.php

Hope that helps you a bit.

pumpkinSoup
September 27th, 2006, 11:08 AM
Thanks for the links :D

JoshuaJonah
September 27th, 2006, 11:58 AM
heres a script you may like:

<?php
$target_path = 'server/path/';
$target_path = $target_path . basename( $_FILES['Filedata']['name']);

move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path);

$thumbsize=65;
$imgfile = $target_path;
list($width, $height) = getimagesize($imgfile);

$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $thumbsize;
$newheight = $thumbsize/$imgratio;
}else{
$newheight = $thumbsize;
$newwidth = $thumbsize*$imgratio;
}
if (function_exists('imagecreatetruecolor')) {
$newimage = ImageCreateTrueColor($newwidth,$newheight);
} else {
$newimage = imagecreate($newwidth,$newheight);
}

if(preg_match("/.jpg/i", "$imgfile"))
{
$format = 'image/jpeg';
}
if (preg_match("/.gif/i", "$imgfile"))
{
$format = 'image/gif';
}
if(preg_match("/.png/i", "$imgfile"))
{
$format = 'image/png';
}

switch($format)
{
case 'image/jpeg':
$source = imagecreatefromjpeg($imgfile);
break;
case 'image/gif';
$source = imagecreatefromgif($imgfile);
break;
case 'image/png':
$source = imagecreatefrompng($imgfile);
break;
}


imagecopyresampled ($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($newimage,"./thumbs/".$_FILES['Filedata']['name'],100);

$thumbsize=350;
$imgfile = $target_path;
list($width, $height) = getimagesize($imgfile);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $thumbsize;
$newheight = $thumbsize/$imgratio;
}else{
$newheight = $thumbsize;
$newwidth = $thumbsize*$imgratio;
}
if (function_exists('imagecreatetruecolor')) {
$newimage = ImageCreateTrueColor($newwidth,$newheight);
} else {
$newimage = imagecreate($newwidth,$newheight);
}
if(preg_match("/.jpg/i", "$imgfile"))
{
$format = 'image/jpeg';
}
if (preg_match("/.gif/i", "$imgfile"))
{
$format = 'image/gif';
}
if(preg_match("/.png/i", "$imgfile"))
{
$format = 'image/png';
}

switch($format)
{
case 'image/jpeg':
$source = imagecreatefromjpeg($imgfile);
break;
case 'image/gif';
$source = imagecreatefromgif($imgfile);
break;
case 'image/png':
$source = imagecreatefrompng($imgfile);
break;
}
if (function_exists('imagecopyresampled')) {
imagecopyresampled ($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
} else {
imagecopyresized($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
imagejpeg($newimage,"./images/".$_FILES['Filedata']['name'],100);


?>

pumpkinSoup
September 27th, 2006, 04:49 PM
Hi.
Joshua that looks like very nice script :D But unfortuantely (as I have said) I know no php. I just copied/ pasted it and uploaded it onto my server, but it just came up with a load of errors (btw my server has php 4.4.4 installed) :( I'm sorry if the answer/ usage is really obvious, I'm a n00b so please forgive me

Could you just give me very simple instructions on how to use that code? Or what it does...

Thanks :D

inzamam
January 20th, 2011, 12:42 AM
if you want crop any image, than i will suggest you to visit the link below
http://www.raiseitsolutions.com/forum/viewtopic.php?f=4&t=2
if you visit this link, you will know how to crop an image easily
thank you very much. :)