Login
网站首页 > 文章中心 > 其它

php图片裁剪与缩放

作者:小编 更新时间:2023-10-06 14:14:25 浏览量:267人看过

我要用PHP做一个缩放、裁剪后台图片的页面,该如何做?

按照我的理解应该是这样的 ,首先你的服务器上面有很多图片,但是图片尺寸不符合使用要求,你想给一个cms使用者制作一个可以裁减缩放图片的功能.

但是第三个步骤是不对的

php求助图片缩放裁切问题

这段代码可以通过自已选择来决定图片的大小!

效果图如下所示:希望对你有帮助!

其中

就可以了!

php

date_default_timezone_set("Asia/Shanghai");

require_once("./image.class.php");

$images?=?new?Images("file");

if?($_GET['act']?==?'cut'){?

$image?=?"0000.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";

html

head

meta?name="Author"?content="SeekEver"

meta?name="Keywords"?content=""

meta?name="Description"?content=""

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({

? ? 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)

? ? {

? ? ? $('#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'

? ? ? });

? ? }

?{

? };

/script

/head

body

form?method="post"?action="?act=upload"?enctype="multipart/form-data"

input?type="file"?name="file"

input?type="submit"?value="上传"

/form

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="裁剪"?/

/body

/html

}?

如何实现PHP图片裁剪与缩放

给你段代码吧.上边的是具体的事务处理.下面的是类文件.

////////////////////////////////////////////////////下面开始处理图片压缩问题

$src = "$fileurl";

echo $src;

$image = new Image($src);

$width= $image-getimgwidth();

echo $width."宽度是这些";

echo $coefficient;

$image-percent = $coefficient;

$image-openImage();

$image-thumpImage();

//************************************重新给图片命名

echo "br/重新命名是这个".$randname;

$fileurl=$fileimgweb.$randname;//重新给数据库里存的图片地址命名

// $image-showImage();

$image-saveImage($fileurl);

////////////////////////////////////////////////////图片压缩问题处理结束

--------------------------------------------------------------------------------------

/**

图片压缩操作类

v1.0

*/

class Image{

private $src;

private $imageinfo;

private $image;

public function __construct($src){

$this-src = $src;

获取图片的宽度并传输给前台

public function getimgwidth(){

$imgwidth= getimagesize($this-src)[0];

// echo $imgwidth;

return $imgwidth;

打开图片

public function openImage(){

list($width, $height, $type, $attr) = getimagesize($this-src);

$this-imageinfo = array(

'width'=$width,

'height'=$height,

'type'=image_type_to_extension($type,false),

'attr'=$attr

);

$fun = "imagecreatefrom".$this-imageinfo['type'];

$this-image = $fun($this-src);

操作图片

public function thumpImage(){

$new_width = $this-imageinfo['width'] * $this-percent;

$new_height = $this-imageinfo['height'] * $this-percent;

$image_thump = imagecreatetruecolor($new_width,$new_height);

//将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度

imagecopyresampled($image_thump,$this-image,0,0,0,0,$new_width,$new_height,$this-imageinfo['width'],$this-imageinfo['height']);

imagedestroy($this-image);

$this-image = $image_thump;

输出图片

public function showImage(){

header('Content-Type: image/'.$this-imageinfo['type']);

$funcs = "image".$this-imageinfo['type'];

$funcs($this-image);

保存图片到硬盘

public function saveImage($fileurl){

// $funcs = "image".$this-imageinfo['type'];

// $funcs($this-image,$name.'.'.$this-imageinfo['type']);

销毁图片

public function destruct(){

以上就是土嘎嘎小编为大家整理的php图片裁剪与缩放相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章