<?php
/*
Author: Uvumi LLC
Website: http://tools.uvumi.com/
License: MIT
Date: 20080910
Notes: You must have the GD library installed for php. On many linux systems this is as simple as doing `yum install php-gd` or `apt-get install php-gd`on the command line as root.
If your site is hosted somewhere else and you don't have root access to the system, then it is very likely that PHP and GD area already available for you.
If you have any questions about this file or how to do this in another server side scripting language, you are welcome to ask us about it on our forum: http://tools.uvumi.com/forum/
Also, you need to define the folders where your images and thumbnails will be stored, and make sure they are either owned by the web server user, or they are world writeable (chmod 777)
*/
$images_path = "images/"; //replace this with the path where your images are stored, relative to the directory this script will run from
$thumbs_path = $images_path . "thumbs/"; //this is you destination thumbmail folder, you can change it if you want, just be sure the folder actually exists and is writeable by the web server. In this example, we don't deal with duplicate names, so we just store the cropped verion in a different folder.
$quality = 80;
// first, we retrieve the form's value, presumably sent through the ost method.
$photo = isset($_POST['filename']) ? $_POST['filename'] : false;
$top = isset($_POST['top']) ? $_POST['top'] : false;
$left = isset($_POST['left']) ? $_POST['left'] : false;
$width = isset($_POST['width']) ? $_POST['width'] : false;
$height = isset($_POST['height']) ? $_POST['height'] : false;
$max_width = isset($_POST['max_width']) ? $_POST['max_width'] : false;
$max_height = isset($_POST['max_height']) ? $_POST['max_height'] : false;
//file_put_contents('1.txt', sprintf('left:%d,top:%d,width:%d,height:%d', $left, $top, $width, $height));
//we set the response header to a JSON string, this is not mandatory, but it's cleaner.
header('Content-type: application/json');
//we make sure all the required parameters are present
if(!($photo && is_numeric($top) && is_numeric($left) && is_numeric($width) && is_numeric($height) && is_numeric($max_height) && is_numeric($max_width))){
die('{"error":"Some of the required parameters are missing."}');
}
//we verify the GD library is running.
if(! extension_loaded('gd')){
die('{"error":"The GD extension is not installed on the server."}');
}
/*
we are going to suppose that $photo contains the source image file name, which it should if you passed it properly from UvumiCrop in the first place.
*/
$source_file = $images_path . $photo;
//we make sure the source path exists
if(!is_dir($images_path)) {
die('{"error":"The source directory could not be found."}');
}
//we make sure the source file exists
if(!file_exists($source_file)) {
die('{"error":"The source image file could not be found."}');
}
//try create the target folder if it doesn't exist
if(!is_dir($thumbs_path)) {
if(!mkdir($thumbs_path,0777,true)){
die('{"error":"The destination directory could not be created."}');
}
}
//Make sure the target folder is writable
if(!is_writable($thumbs_path)){
die('{"error":"The server does not have permission to write in the destination folder."}');
}
//We get the file extension from the file name. It's needed later
$filename = explode('.', $photo);
$extension = array_pop($filename);
//we create a new filename from the original with the "thumb" suffix.
$thumb = implode('.',$filename). rand() . "-thumb." . $extension;
//the full target path + file name
$target_file = $thumbs_path . $thumb;
$info = getimagesize($source_file);
if(!$info){
die('{"error":"The file type is not supported."}');
}
// we use the the GD library to load the image, using the file extension to choose the right function
switch($info[2]) {
case IMAGETYPE_GIF:
if(!$source_image = imagecreatefromgif($source_file)){
die('{"error":"Could not open GIF file."}');
}
break;
case IMAGETYPE_PNG:
if(!$source_image = imagecreatefrompng($source_file)){
die('{"error":"Could not open PNG file."}');
}
break;
case IMAGETYPE_JPEG:
if(!$source_image = imagecreatefromjpeg($source_file)){
die('{"error":"Could not open JPG file."}');
}
break;
default:
die('{"error":"The file type is not supported."}');
break;
}
//Calculate the new size based on the selected area and the minimums
if($width > $height) {
$dest_width = $max_width;
$dest_height = round($max_width*$height/$width);
} else {
$dest_width = round($max_height*$width/$height);
$dest_height = $max_height;
}
//we generate a new image object of the size calculated above, using PHP's GD functions
if(!$dest_image = imagecreatetruecolor($dest_width, $dest_height)){
die('{"error":"Could not create new image from source file."}');
}
//hack to keep transparency in gif and png
if($info[2]==IMAGETYPE_GIF||$info[2]==IMAGETYPE_PNG){
if($info[2]==IMAGETYPE_PNG){
imageAntiAlias($dest_image,true);
}
imagecolortransparent($dest_image, imagecolorallocatealpha($dest_image, 0, 0, 0,127));
imagealphablending($dest_image, false);
imagesavealpha($dest_image, true);
}
/*
this is where we crop the image,
-the first parameter is the destinatation image (not a physical file, but a GD image object)
-second is the source image. Again it's not the physical file but a GD object (which was generated from an image file this time)
-third and fourth params are the X and Y coordinates to paste the copied region in the destination image. In this case we want both of them to be 0,
-fifth and sixth are the X and Y coordinates to start cropping in the source image. So they are pretty much the coordinates we got from UvumiCrop.
-seventh and eighth are the width and height of the destination image, the one calculated right above
-ninth and tenth are the width and height of the cropping region, directly from UvumiCrop again
By just setting $max_width and $max_height above, you should not have to worry about this
*/
if(!imagecopyresampled($dest_image, $source_image, 0, 0, $left, $top, $dest_width, $dest_height, max($width, $max_width), max($height, $max_height))){
die('{"error":"Could not crop the image with the provided coordinates."}');
}
//just as we used $extension to pick the loading function, we'll use it again here to determine which GH function we need for outputting the cropped image
switch($info[2]) {
case IMAGETYPE_GIF:
if(!imagegif($dest_image, $target_file)){
die('{"error":"Could not save GIF file."}');
}
break;
case IMAGETYPE_PNG:
if(!imagepng($dest_image, $target_file, max(9 - floor($quality/10),0))){
die('{"error":"Could not save PNG file."}');
}
break;
case IMAGETYPE_JPEG:
if(!imagejpeg($dest_image, $target_file, $quality)){
die('{"error":"Could not save JPG file."}');
}
break;
}
imagedestroy($dest_image);
imagedestroy($source_image);
//If we made it that far with no error, we can return a success message, with the thumb filename
die('{"success":"'.$thumb.'"}');
?>
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
crop.zip (80个子文件)
crop
save-thumb1.php 7KB
images
anbottonbj.gif 429B
thumbs
prompt.gif 1KB
1.jpg 67KB
aa.jpg 149KB
2.jpg 31KB
test.html 2KB
save-thumb.php 1014B
js
mootools-1.2.4.4-more.js 204KB
ImageCutter
images
anbottonbj.gif 429B
prompt.gif 1KB
ImageCutter.css 831B
ImageCutter.js 6KB
ext.js 3KB
UvumiTools
UvumiCrop.js 37KB
UvumiCrop-compressed.js 22KB
css
handle-nw.gif 223B
handle-ns.gif 219B
handle-ew.gif 220B
blank.gif 43B
handle.gif 224B
uvumi-crop.css 3KB
handle-ne.gif 222B
mootools-1.2.4-core-nc.js 101KB
sexy_lightbox
sexylightbox.v2.3.mootools.min.js 10KB
demo-shake.html 126B
images
user.png 2KB
small_clapclap.jpg 4KB
small_homerin2.jpg 3KB
neuquen.jpg 167KB
yo.gif 261KB
iguazu02.jpg 88KB
small_neuquen.jpg 6KB
small_iguazu02.jpg 5KB
yo.jpg 4KB
2007_Audi_R8_01.jpg 463KB
homerin2.jpg 46KB
php_mysql.jpg 24KB
small_2007_Audi_R8_01.jpg 3KB
ryu.gif 167KB
small_ryu.jpg 4KB
clapclap.jpg 38KB
small_php_mysql.jpg 4KB
dialog-help.png 4KB
small_puente_mujer.jpg 5KB
puente_mujer.jpg 59KB
demo-alert.php 728B
demo-data.html 402B
demo-login.html 235B
sexylightbox.v2.3.mootools.js 24KB
demo-iframe.html 774B
index-white.html 14KB
sexyimages
black
bgSexy.gif.png 506B
buttons.png 2KB
404html.png 3KB
bgSexy.png 1KB
404.png 3KB
loading.gif 1KB
blanco
bgSexy.gif 1KB
buttons.png 734B
404html.png 3KB
bgSexy.png 1KB
404.png 3KB
loading.gif 2KB
white
bgSexy.gif 506B
buttons.png 728B
404html.png 3KB
bgSexy.png 1KB
404.png 3KB
loading.gif 1KB
negro
bgSexy.gif 1KB
buttons.png 2KB
404html.png 3KB
bgSexy.png 2KB
404.png 3KB
loading.gif 2KB
index.html 16KB
sexylightbox.css 2KB
global.css 5KB
download-thumb.php 5KB
共 80 条
- 1
资源评论
- zxz_java2012-06-11无法运行,还是去官网看吧。http://uvumitools.com/crop.html
- VIPStone2012-11-29哥呀!这分儿有点贵!!没有达到预期效果,不过还是感谢分享
- ipsoft2013-05-29还不错吧。就是贵了点
握固
- 粉丝: 32
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功