php裁切图片,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 = “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”;

 }

}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=”0000.jpg”  /div

 div style=”width:48px;height:48px;margin:10px;overflow:hidden; float:left;”img  style=”float:left;” id=”preview” src=”0000.jpg” /div

 div style=”width:190px;height:195px;margin:10px;overflow:hidden; float:left;”img  style=”float:left;” id=”preview2″ src=”0000.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做图片裁切

PHP做图片裁剪的原理是通过前台定位出要裁剪图片的左上角位置,及右下角位置的做标,如果有放大和缩小的话还要给出放大和缩小的百分比,以及要对哪一个图片进行操作,这几个值传到后台,在用PHP去处理!

前端图片坐标点的得到可以自己用JS去实现,如果你的JS写的不是很熟也没有关系,可以下载一个jsCropperUI这一个包里面有很多的DEMO,可以实现前台的拖曳效果了!

前台会有一个AJAX向后台发送数据,一般格式为x1=”+position[0]+”y1=”+position[1]+”x2=”+position[2]+”y2=”+position[3]+”r=”+position[4]+”id = imgid四个坐标,缩放值,要操作的ID

后PHP接收这个数据后,进行操作

$x1 = $_GET[‘x1’];

$y1 = $_GET[‘y1’];

$x2 = $_GET[‘x2’];

$y2 = $_GET[‘y2’];

$r = $_GET[‘r’];

if($x2$x1||$y2$y1){

return false;

}

$img=”dog.jpg”;

$im=imagecreatefromjpeg($img);

$percent = $r*0.01;

list($width, $height) = getimagesize($img);

$new_width = $width * $percent;

$new_height = $height * $percent;

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

imagecopyresampled($tempimg, $im,0,0,0,0, $new_width,$new_height, $width, $height);

imagejpeg($tempimg,”dog_temp.jpg”);

ImageDestroy ($im);

$ims=$tempimg;

$srcW=ImageSX($ims);

$srcH=ImageSY($ims);

$top_x = $x1;

$top_y = $y1;

$bottom_x = $x2;

$bottom_y = $y2;

$newimg = imagecreatetruecolor($bottom_x-$top_x,$bottom_y-$top_y);

imagecopyresampled($newimg, $ims,0,0,$top_x,$top_y, $srcW,$srcH, $srcW, $srcH);

imagejpeg($newimg,”dogs.jpg”);

ImageDestroy ($ims);

echo 1;

成功后输出1让前台得到进行裁剪区域的刷新!这样就可以实现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 图片下载本地并进行裁剪

?php

//剪裁图片

//1,打开图像

//2,创建一个空白的图像

$name=”./img/1.jpg”;

 // cut($name,0,0,200,200);

$x1=289;

$y1=149;

$x2=438;

$y2=257;

cut($name,289,149,438,257);

 function cut($name,$x1,$y1,$x2,$y2){

function getinfo($name){

//getimagesize($dname)函数可以动态的获取图片类型,大小,宽度和高度等

$info=getimagesize($name);

$width=$info[0];

$height=$info[1];

$mime=$info[‘mime’];

 

switch ($mime) {

case ‘image/jpeg’:

$res=imagecreatefromjpeg($name);

break;

case ‘image/gif’:

$res=imagecreatefromgif($name);

break;

case ‘image/png’:

$res=imagecreatefrompng($name);

break;

case ‘image/wbmp’:

$res=imagecreatefromwbmp($name);

break;

}

return array(‘width’=$width,’height’=$height,’res’=$res);

}

$info=getinfo($name);

 

$img=imagecreatetruecolor(($x2-$x1),($y2-$y1));

imagecopymerge($img,$info[‘res’],0,0,$x1,$y1,($x2-$x1),($y2-$y1),100);

// header(“content-type:image/png”);

// imagepng($img);

$ext=pathinfo($name,PATHINFO_EXTENSION);

$rand_name=md5(mt_rand().time()).”.”.$ext;

switch($ext){

case ‘jpg’:

case ‘jpeg’:

case ‘jpe’:

imagejpeg($img,$rand_name);

break;

case ‘png’:

imagepng($img,$rand_name);

break;

case ‘gif’:

imagegif($img,$rand_name);

break;

case ‘bmp’:

case ‘wbmp’:

imagewbmp($img,$rand_name);

break;

}

//销毁资源

imagedestroy($info[‘res’]);

imagedestroy($img);

}

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/309245.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝的头像小蓝
上一篇 2025-01-04 19:30
下一篇 2025-01-04 19:30

相关推荐

  • PHP和Python哪个好找工作?

    PHP和Python都是非常流行的编程语言,它们被广泛应用于不同领域的开发中。但是,在考虑择业方向的时候,很多人都会有一个问题:PHP和Python哪个好找工作?这篇文章将从多个方…

    编程 2025-04-29
  • 用Python绘制酷炫图片

    在本篇文章中,我们将展示如何使用Python绘制酷炫的图片。 一、安装Python绘图库 在使用Python绘制图片之前,我们需要先安装Python绘图库。Python有很多绘图库…

    编程 2025-04-29
  • 使用axios获取返回图片

    使用axios获取返回图片是Web开发中很常见的需求。本文将介绍如何使用axios获取返回图片,并从多个方面进行详细阐述。 一、安装axios 使用axios获取返回图片前,首先需…

    编程 2025-04-29
  • Python 图片转表格

    本文将详细介绍如何使用Python将图片转为表格。大家平时在处理一些资料的时候难免会遇到图片转表格的需求。比如从PDF文档中提取表格等场景。当然,这个功能也可以通过手动复制、粘贴,…

    编程 2025-04-29
  • PHP怎么接币

    想要在自己的网站或应用中接受比特币等加密货币的支付,就需要对该加密货币拥有一定的了解,并使用对应的API进行开发。本文将从多个方面详细阐述如何使用PHP接受加密货币的支付。 一、环…

    编程 2025-04-29
  • Python缓存图片的处理方式

    本文将从多个方面详细阐述Python缓存图片的处理方式,包括缓存原理、缓存框架、缓存策略、缓存更新和缓存清除等方面。 一、缓存原理 缓存是一种提高应用程序性能的技术,在网络应用中流…

    编程 2025-04-29
  • Python如何抓取图片数据

    Python是一门强大的编程语言,能够轻松地进行各种数据抓取与处理。抓取图片数据是一个非常常见的需求。在这篇文章中,我们将从多个方面介绍Python如何抓取图片数据。 一、使用ur…

    编程 2025-04-29
  • Avue中如何按照后端返回的链接显示图片

    Avue是一款基于Vue.js、Element-ui等技术栈的可视化开发框架,能够轻松搭建前端页面。在开发中,我们使用到的图片通常都是存储在后端服务器上的,那么如何使用Avue来展…

    编程 2025-04-28
  • Python利用Image加图片的方法

    在Python中,利用Image库可以快速处理图片,并加入需要的图片,本文将从多个方面详细阐述这个操作。 一、Image库的安装和基础操作 首先,我们需要在Python中安装Ima…

    编程 2025-04-28
  • 使用CKSlide实现图片轮播

    CKSlide是一个基于jQuery的插件,可以方便地为网页添加幻灯片和图片轮播效果。使用CKSlide可以让网站更加生动、活泼,给用户带来更好的体验。 一、CKSlide基本用法…

    编程 2025-04-28

发表回复

登录后才能评论