很多站长朋友们都不太清楚php裁切图片,今天小编就来给大家整理php裁切图片,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php求助图片缩放裁切问题 2、 php如何实现图片的裁剪 3、 为什么用PHP对上传图片进行裁剪的时候,没成功,而且连上传的原图都没了 4、 PHP图像处理函数有哪些 5、 怎么实现php上传图片并可以裁剪的功能,类似一些网站的头像截取,裁剪可以用jcrop插件。高分悬赏 6、 PHP 长方形图片 不变形 裁剪 生成 正方形 php求助图片缩放裁切问题这段代码可以通过自已选择来决定图片的大小!
效果图如下所示:希望对你有帮助!
其中
minSize: [48,48],
setSelect: [0,0,190,190],
是调整选取范围的大小,若你 调整为120和160就改为了
setSelect: [0,0,120,160],
就可以了!
<?php
error_reporting(7);
date_default_timezone_set("Asia/Shanghai");
header("Content-type:text/html; Charset=utf-8");
require_once("./image.class.php");
$images = new Images("file");
if ($_GET['act'] == 'cut'){
$image = "/data/upload/help/202303/13/ad7ffe963687c817362beb2b4764e277.jpg";
$res = $images->thumb($image,false,1);
if($res == false){
echo "裁剪失败";
}elseif(is_array($res)){
echo '<img src="'.$res['big'].'" style="margin:10px;">';
echo '<img src="'.$res['small'].'" style="margin:10px;">';
}elseif(is_string($res)){
echo '<img src="'.$res.'">';
}
}elseif(isset($_GET['act']) $_GET['act'] == "upload"){
$path = $images->move_uploaded();
$images->thumb($path,false,0); //文件比规定的尺寸大则生成缩略图,小则保持原样
if($path == false){
$images->get_errMsg();
}else{
echo "上传成功!<a href='".$path."' target='_blank'>查看</a>";
}
}else{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ";quot;;>
<html>
<head>
<meta name="Author" content="SeekEver">
<meta name="Keywords" content="">
<meta name="Description" content="">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<script src="./js/jquery.min.js" type="text/javascript"></script>
<script src="./js/jquery.Jcrop.js" type="text/javascript"></script>
<link rel="stylesheet" href="./css/jquery.Jcrop.css" type="text/css" />
<script type="text/javascript">
jQuery(function($){
// Create variables (in this scope) to hold the API and image size
var jcrop_api, boundx, boundy;
$('#target').Jcrop({
minSize: [48,48],
setSelect: [0,0,190,190],
onChange: updatePreview,
onSelect: updatePreview,
onSelect: updateCoords,
aspectRatio: 1
},
function(){
// Use the API to get the real image size
var bounds = this.getBounds();
boundx = bounds[0];
boundy = bounds[1];
// Store the API in the jcrop_api variable
jcrop_api = this;
});
function updateCoords(c)
{
$('#x').val(c.x);
$('#y').val(c.y);
$('#w').val(c.w);
$('#h').val(c.h);
};
function checkCoords()
{
if (parseInt($('#w').val())) return true;
alert('Please select a crop region then press submit.');
return false;
};
function updatePreview(c){
if (parseInt(c.w) > 0)
{
var rx = 48 / c.w; //小头像预览Div的大小
var ry = 48 / c.h;
$('#preview').css({
width: Math.round(rx * boundx) + 'px',
height: Math.round(ry * boundy) + 'px',
marginLeft: '-' + Math.round(rx * c.x) + 'px',
marginTop: '-' + Math.round(ry * c.y) + 'px'
});
}
{
var rx = 199 / c.w; //大头像预览Div的大小
var ry = 199 / c.h;
$('#preview2').css({
width: Math.round(rx * boundx) + 'px',
height: Math.round(ry * boundy) + 'px',
marginLeft: '-' + Math.round(rx * c.x) + 'px',
marginTop: '-' + Math.round(ry * c.y) + 'px'
});
}
};
});
</script>
</head>
<body>
<form method="post" action="?act=upload" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
<div style="float:left;"><img id="target" src="/data/upload/help/202303/13/ad7ffe963687c817362beb2b4764e277.jpg" ></div>
<div style="width:48px;height:48px;margin:10px;overflow:hidden; float:left;"><img style="float:left;" id="preview" src="/data/upload/help/202303/13/ad7ffe963687c817362beb2b4764e277.jpg" ></div>
<div style="width:190px;height:195px;margin:10px;overflow:hidden; float:left;"><img style="float:left;" id="preview2" src="/data/upload/help/202303/13/ad7ffe963687c817362beb2b4764e277.jpg" ></div>
<form action="index.php?act=cut" method="post" onsubmit="return checkCoords();">
<input type="hidden" id="x" name="x" />
<input type="hidden" id="y" name="y" />
<input type="hidden" id="w" name="w" />
<input type="hidden" id="h" name="h" />
<input type="submit" value="裁剪" />
</form>
</body>
</html>
<?php
}
?>
php如何实现图片的裁剪php中裁剪图片主要使用gd库的imagecopyresampled方法
$src_path = '1.jpg';
//创建源图的实例
$src = imagecreatefromstring(file_get_contents($src_path));
//裁剪开区域左上角的点的坐标
$x = 100;
$y = 12;
//裁剪区域的宽和高
$width = 200;
$height = 200;
//最终保存成图片的宽和高,和源要等比例,否则会变形
$final_width = 100;
$final_height = round($final_width * $height / $width);
//将裁剪区域复制到新图片上,并根据源和目标的宽高进行缩放或者拉升
$new_image = imagecreatetruecolor($final_width, $final_height);
imagecopyresampled($new_image, $src, 0, 0, $x, $y, $final_width, $final_height, $width, $height);
//输出图片
header('Content-Type: image/jpeg');
imagejpeg($new_image);
imagedestroy($src);
imagedestroy($new_image);
为什么用PHP对上传图片进行裁剪的时候,没成功,而且连上传的原图都没了加个base64_decode方法试试:
$src = imagecreatefromstring(base64_decode(file_get_contents($src_path)));
PHP图像处理函数有哪些php图像处理函数大全
php图片处理代码分享,包括缩放、剪裁、缩放、翻转、旋转、透明、锐化等。需要的朋友可以参考下
一、创建图片资源
imagecreatetruecolor(width,height);
imagecreatefromgif(图片名称);
imagecreatefrompng(图片名称);
imagecreatefromjpeg(图片名称);画出各种图像
imagegif(图片资源,保存路径);
imagepng()
imagejpeg();
二、获取图片属性
imagesx(res//宽度
imagesy(res//高度
getimagesize(文件路径)
返回一个具有四个单元的数组。索引
0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 =
PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10
= JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。这些标记与 PHP 4.3.0 新加的
IMAGETYPE 常量对应。索引 3 是文本字符串,内容为“height="yyy" width="xxx"”,可直接用于 IMG
标记。
销毁图像资源
imagedestroy(图片资源);
三、透明处理
PNG、jpeg透明色都正常,只有gif不正常
imagecolortransparent(resource
image [,int
color])//将某个颜色设置成透明色
imagecolorstotal()
imagecolorforindex();
四、图片的裁剪
imagecopyresized()
imagecopyresampled();
五、加水印(文字、图片)
字符串编码转换string iconv ( string $in_charset ,
string $out_charset , string $str )
六、图片旋转
imagerotate();//制定角度的图片翻转
七、图片的翻转
沿X轴 沿Y轴翻转
八、锐化
imagecolorsforindex()
imagecolorat()
怎么实现php上传图片并可以裁剪的功能,类似一些网站的头像截取,裁剪可以用jcrop插件。高分悬赏php本身有裁剪图片的函数,js的截取一般是获得几个坐标,供这个函数作为参数,php在图片上传到服务器临时空间的时候,对图片进行裁剪,再按编程人的需求保存到指定目录。
百度下现成的,或者翻翻手册。
PHP 长方形图片 不变形 裁剪 生成 正方形$x = (200-150)/2;
imagecopyresampled($thumb, $img_r2, 0, 0, $x, 0, $thumb_w, $thumb_h, 150, 150);
请看imagecopyresampled详细介绍:
imagecopyresampled
(PHP 4 >= 4.0.6, PHP 5)
imagecopyresampled — 重采样拷贝部分图像并调整大小
说明
bool imagecopyresampled ( resource $dst_image, resource $src_image, int $dst_x, int $dst_y, int $src_x, int $src_y, int $dst_w, int $dst_h, int $src_w, int $src_h )
imagecopyresampled() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。如果成功则返回 TRUE,失败则返回 FALSE。
dst_image 和 src_image 分别是目标图像和源图像的标识符。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如果 dst_image 和 src_image 相同的话)区域,但如果区域交迭的话则结果不可预知。
注意: 因为调色板图像限制(255+1 种颜色)有个问题。重采样或过滤图像通常需要多于 255 种颜色,计算新的被重采样的像素及其颜色时采用了一种近似值。对调色板图像尝试分配一个新颜色时,如果失败我们选择了计算结果最接近(理论上)的颜色。这并不总是视觉上最接近的颜色。这可能会产生怪异的结果,例如空白(或者视觉上是空白)的图像。要跳过这个问题,请使用真彩色图像作为目标图像,例如用 imagecreatetruecolor() 创建的。
注意: 本函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。
关于php裁切图片的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。