You are here: Re: reducing file size? « All PHP « IT news, forums, messages
Re: reducing file size?

Posted by Geoff Berrow on 03/17/07 00:48

Message-ID: <1174089479.184471.247800@n76g2000hsh.googlegroups.com> from
Kentor contained the following:

>Summary: how can I reduce a file with PHP?

Assuming a form with a file upload box called userfile and two writeable
folders called main and thumb

<?php
$mainpath="main/";
$thumbpath = "thumb/";
$thumbnail_width=80;
$main_width=450;
$enlarge=800;
$max_size = 2000000;
$filename="";
$time=time();

if (!empty($_FILES['userfile'])) {

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {

if ($_FILES['userfile']['size']>$max_size)
{ echo "The file is too big<br>\n"; exit; }

if($_FILES['userfile']['type']=="image/gif"){
$createfunction="imagecreatefromgif";}
else{$createfunction="imagecreatefromjpeg";}

if (($_FILES['userfile']['type']=="image/gif") ||
($_FILES['userfile']['type'])=="image/pjpeg" ||
($_FILES['userfile']['type']=="image/jpeg")) {

//create thumbnail image
$src_img = $createfunction($_FILES['userfile']['tmp_name']);
$wid=imagesx($src_img);
$hei=imagesy($src_img);
if($hei>$wid){
$new_h = $thumbnail_width;
$new_w = ($new_h*$wid)/$hei;
}
else{
$new_w = $thumbnail_width;
$new_h=($new_w*$hei)/$wid;
}
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w,
$new_h, imagesx($src_img), imagesy($src_img));
imagejpeg(UnsharpMask($dst_img, 130, 0.8, 3), $thumbpath
.."s_".$time.$_FILES['userfile']['name'], 100);

//create big image
if(imagesx($src_img)>$main_width){
$wid=imagesx($src_img);
$hei=imagesy($src_img);
if($hei>$wid){
$new_h = $main_width;
$new_w = ($new_h*$wid)/$hei;
}
else{
$new_w = $main_width;
$new_h=($new_w*$hei)/$wid;
}
$dst_img = imagecreatetruecolor($new_w,$new_h);
$res=imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0,
$new_w, $new_h, imagesx($src_img), imagesy($src_img));
imagejpeg(UnsharpMask($dst_img, 50, 0.5, 3), $mainpath
.."b_".$time.
$_FILES['userfile']['name'], 90);
}
else{
$res = copy($_FILES['userfile']['tmp_name'], $mainpath
.."b_".$time.
$_FILES['userfile']['name']);
}
//create large image
if(imagesx($src_img)>$enlarge){
$wid=imagesx($src_img);
$hei=imagesy($src_img);
if($hei>$wid){
$new_h = $enlarge;
$new_w = ($new_h*$wid)/$hei;
}
else{
$new_w = $enlarge;
$new_h=($new_w*$hei)/$wid;
}
$dst_img = imagecreatetruecolor($new_w,$new_h);
$res=imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0,
$new_w, $new_h, imagesx($src_img), imagesy($src_img));
imagejpeg(UnsharpMask($dst_img, 50, 0.5, 3), $mainpath
.."e_".$time.
$_FILES['userfile']['name'], 90);
}
else{
$res = copy($_FILES['userfile']['tmp_name'], $mainpath
.."e_".$time.
$_FILES['userfile']['name']);
}
if (!$res) { echo "<h2>Upload failed!</h2><br>\n"; exit;
}
else { $success= "<h2>Image upload sucessful!</h2>\n";
}

$filename=$_FILES['userfile']['name'];
}

else { echo "<span>Please use a .jpg or a .gif
file</span><br>\n"; exit; }

imagedestroy($src_img);
imagedestroy($dst_img);
}
//end of image upload

//variables for insertion into database table
$thumb_url="s_".$time.$filename;
$main_url="b_".$time.$filename;



}

?>

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация