imagecopy php,PHP: imagecopyresized - Manual

If you need to delete or resize images in the filesystem (not in DB) without loosing the image quality...

I commented the code as much as possible so that newbies (like myself) will understand it.  ;)

WRITTEN BY:

Finnur Eiriksson, (http://www.centrum.is/finnsi)

Based on snippets that have been posted on www.PHP.net

Drop me an e-mail if you have any questions.

NOTE:

This code is written to either delete or resize pictures in the file system, so if you have your pictures in a database

you will have to make some changes. Also, if you are using other picture formats than .gif's or .jpg's you

will have to add som code as well (Read the comments to find out where to do this).

IMPORTANT:

The $_GET['resizepic'] variable only contains the NAME of the file that is going to be deleted/resized.

The internet guest account (IUSR_SERVERNAME on WINDOWS) must have read and write permissions (execution permission not needed)

in your image directory (i.e. $dir_name = "FooBar"). It is a good idea to have a separate directory for images that users

can upload to and manipulate the contents. Ideally, you should have one directory for the pictures that are used for the website,

and another upload directory

*/$dir_name="FooBar";// Enter the name of the directory that contains the images$olddir=getcwd();// Get the Current Windows Directory to be able to switch back in the end of the script$dir=opendir($dir_name);//make a directory handle

//To delete a pictureif(isset($_GET['delpic'])){chdir('images');$delpic=$_GET['delpic'];

@unlink($delpic);chdir($olddir);

}//To resize a pictureif(isset($_GET['resize'])){//$_GET['resize'] contains the resize-percentage (for example 80 and 40, for 80% and 40% respectively. To double the image in size the user enters 200 etc.)

// File and new size$percent= ($_GET['resize']/100);chdir('images');// change the windows directory to the image directory$filename=$_GET['resizepic'];// Decide the content type, NB:This code is written to only execute on .gif's and .jpg's

// If you want other formats than .gif's and .jpg's add your code here, in the same manner:$format='';

if(preg_match("/.jpg/i","$filename")){$format='image/jpeg';header('Content-type: image/jpeg');

}

if (preg_match("/.gif/i","$filename")){$format='image/gif';header('Content-type: image/gif');

}

if($format!=''){//This is where the actual resize process begins...

// Get new sizeslist($width,$height) =getimagesize($filename);$newwidth=$width*$percent;$newheight=$height*$percent;// Load the imageswitch($format){

case'image/jpeg':$source=imagecreatefromjpeg($filename);

break;

case'image/gif';$source=imagecreatefromgif($filename);

break;

}//Get the Image$thumb=imagecreatetruecolor($newwidth,$newheight);//This must be set to false in order to be able to overwrite the black

//pixels in the background with transparent pixels. Otherwise the new

//pixels would just be applied on top of the black background.imagealphablending($thumb,false);//Make a temporary file handle$source= @imagecreatefromjpeg($filename);// Resizeimagecopyresized($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);//Write the image to the destination file@imagejpeg($thumb,$filename);//Change back to the old directory... I'm not sure that this is neccessarychdir($olddir);//Specify where you want the user to go after the operation:header('Location: foobar.php');

}

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值