473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

PHP + Upload + picture script

I was wondering if anyone does any uploading picture scripts or how to
do it where i could upload a picture and use php to change the picture
size to what i want it to be. Anyone know where i could learn how to do
this or have any script examples? i would really appreciate it alot.

thanks for you time

jason

Oct 30 '06 #1
5 1871
For the uploading part, here's some basic code:
<?php
if(@$_POST['MAX_FILE_SIZE']){ // If it is actually uploading
something...
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
//SET YOUR OWN UPLOAD DIR (absolute)
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){
echo "File was successfully uploaded!\n You can access the file at:
http://".ROOT_DOMAIN.$uploaddir.basename($_FILES['userfile']['name'])."\n";
} else {
echo "Error! File was not uploaded!\n";
};
};
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?admin=<?php echo
ADMIN_PAGE; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="userfile" /><br />
<input type="submit" value="Upload" />
</form>

You can find a basic image resizing script @ php.net under GD2
functions -

<?php
function CreateThumb($file,$maxwdt,$maxhgt, $dest) {
list($owdt,$ohgt,$otype)=@getimagesize($file);

switch($otype) {
case 1: $newimg=imagecreatefromgif($file); break;
case 2: $newimg=imagecreatefromjpeg($file); break;
case 3: $newimg=imagecreatefrompng($file); break;
default: echo "Unkown filetype (file $file, typ $otype)"; return;
}

if($newimg) {
if($owdt>1500 || $ohgt>1200)
list($owdt, $ohgt) = Resample($newimg, $owdt, $ohgt,
1024,768,0);

Resample($newimg, $owdt, $ohgt, $maxwdt, $maxhgt);

if(!$dest) return $newimg;

if(!is_dir(dirname($dest)))
mkdir(dirname($dest));

switch($otype) {
case 1: imagegif($newimg,dest); break;
case 2: imagejpeg($newimg,$dest,90); break;
case 3: imagepng($newimg,$dest); break;
}

imagedestroy($newimg);

chmod($dest,0644);
}
}

function Resample(&$img, $owdt, $ohgt, $maxwdt, $maxhgt, $quality=1) {
if(!$maxwdt) $divwdt=0;
else $divwdt=Max(1,$owdt/$maxwdt);

if(!$maxhgt) $divhgt=0;
else $divhgt=Max(1,$ohgt/$maxhgt);

if($divwdt>=$divhgt) {
$newwdt=$maxwdt;
$newhgt=round($ohgt/$divwdt);
} else {
$newhgt=$maxhgt;
$newwdt=round($owdt/$divhgt);
}

$tn=imagecreatetruecolor($newwdt,$newhgt);
if($quality)

imagecopyresampled($tn,$img,0,0,0,0,$newwdt,$newhg t,$owdt,$ohgt);

else
imagecopyresized($tn,$img,0,0,0,0,$newwdt,$newhgt, $owdt,$ohgt);

imagedestroy($img);

$img = $tn;

return array($newwdt, $newhgt);
}
?>

just use the function create thumb to set the dimensions and the
destination file

*There is a script that will work out the % size for you on php.net, if
you need %s*

On Oct 30, 5:19 pm, "jason.ta...@gmail.com" <jason.ta...@gmail.com>
wrote:
I was wondering if anyone does any uploading picture scripts or how to
do it where i could upload a picture and use php to change the picture
size to what i want it to be. Anyone know where i could learn how to do
this or have any script examples? i would really appreciate it alot.

thanks for you time

jason
Oct 30 '06 #2
<ja*********@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>I was wondering if anyone does any uploading picture scripts or how to
do it where i could upload a picture and use php to change the picture
size to what i want it to be. Anyone know where i could learn how to do
this or have any script examples? i would really appreciate it alot.

thanks for you time

jason
I developed a little script that I used only for jpegs so far. I haven't
tested it with other image types. Here it is:

function thumbnail($img, $w_d, $h_d) {
// $img is the image
// $w_d is the desired width in pixels
// $h_d is the desired height in pixels
//
// function to get the width and height to display q thumbnail
// Pass in the image location, the deisred width and the desired height
// Ouput is an array of height and width to display that can be
// exploded with the list commant
list($w, $h, $type, $attr) = getimagesize($img);
$calc_width = $h_d * $w / $h;
$calc_height = $w_d * $h / $w;
if ($calc_width <= $w_d) {
$disp_width = $calc_width;
$disp_height = $h_d;
} else {
$disp_width = $w_d;
$disp_height = $calc_height;
}
return array($disp_width, $disp_height);
}
I call it with:

list($disp_width, $disp_height) = thumbnail($the_picture_location, 500,
500);

and in the html area I have:

<img src="the-picture-location" width="<?php echo $disp_height;?>"
height="<?php echo $disp_height;?>"
border="0" alt="something here" align="bottom" />

Hope that helps.

Shelly


Oct 30 '06 #3
>
On Oct 30, 5:19 pm, "jason.ta...@gmail.com" <jason.ta...@gmail.com>
wrote:
I was wondering if anyone does any uploading picture scripts or how to
do it where i could upload a picture and use php to change the picture
size to what i want it to be. Anyone know where i could learn how to do
this or have any script examples? i would really appreciate it alot.

thanks for you time

jason

Take a look on the phpclasses.org site. You will need to register but
I've never had a problem from there.
There are some excellent and well documented class files that can be
used to handle image resize (amongst other things) and by reading over
the code you can learn how they work while solving a problem straight
away.
Hope that helps
b

Oct 31 '06 #4
ja*********@gmail.com wrote:
I was wondering if anyone does any uploading picture scripts or how to
do it where i could upload a picture and use php to change the picture
size to what i want it to be. Anyone know where i could learn how to
do this or have any script examples? i would really appreciate it
alot.
Actually, I developed just that type of function for a member system
where members would be able to upload a photo of themselves.

Here's the code snippet along with usage:
http://dev.bd0.net/test/image_upload_convert.phps

The function will accept JPEG, GIF and PNG files as input and by
default output a JPEG file.

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Oct 31 '06 #5
aussiebob schreef:
>On Oct 30, 5:19 pm, "jason.ta...@gmail.com" <jason.ta...@gmail.com>
wrote:
>>I was wondering if anyone does any uploading picture scripts or how to
do it where i could upload a picture and use php to change the picture
size to what i want it to be. Anyone know where i could learn how to do
this or have any script examples? i would really appreciate it alot.

thanks for you time

jason


Take a look on the phpclasses.org site. You will need to register but
I've never had a problem from there.
There are some excellent and well documented class files that can be
used to handle image resize (amongst other things) and by reading over
the code you can learn how they work while solving a problem straight
away.
Hope that helps
b

I agree ... I just posted a modified script in alt.php that I downloaded
from phpclasses that exactly fits your needs
Oct 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phillip Parr | last post by:
I'm trying to get a file upload to work and it's not happening. Here's my form code... echo '<form enctype="multipart/form-data" action="catalogue.php" method="post"> <input type="hidden"...
2
by: Ken | last post by:
I am trying to upload a file to an Apache2 server on Windows 2000 Pro. The temp file name is being added to the database. Everything seems to work except the file is not actually transferred to...
2
by: NotGiven | last post by:
Please help me understand the big picture of allowing users to upload pictures and keep them separate and tied to their record in the database. I want the whole thing automated and I'm just...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
7
tolkienarda
by: tolkienarda | last post by:
hi all I am using a php script to try to upload images to my database. i know i am connecting to my database because the image title is being wirtten to the database. but the image isn't example:...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
6
by: aanund | last post by:
I don't know if I'm in the right place, or doing this properly, but I hope someone sees it anyway. I am a rookin i PHP, yet I have found this to be an efficient and useful scripting language for many...
3
by: thesti | last post by:
hi, i dunno why, but i have tested my upload script with IE 6. but the file wasn't uploaded. it works fine with Opera and FF. i use php 5.1.2 here is my upload script //get the...
24
by: owz2008 | last post by:
This has probably been covered before but could not find a similar thread. Basically I have created a form which can be viewed at www.icomworks.co.uk/canvaspayform.html I want to submit the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.