使用php裁剪图像

使用php裁剪图像,php,image,crop,Php,Image,Crop,我想裁剪一张图像。有可能这样做吗。如果是,那么crop()函数会是什么样子 $imgURL始终是.jpg图像 $image = file_get_contents($imgURL); $maxWidth = 100; $height = 68; $image = crop($image, $maxWidth, $height); file_put_contents("media/imgname.jpg", $image); function crop($image, $maxWidth,

我想裁剪一张图像。有可能这样做吗。如果是,那么
crop()
函数会是什么样子

$imgURL
始终是
.jpg
图像

$image = file_get_contents($imgURL);
$maxWidth = 100;
$height = 68;

$image = crop($image, $maxWidth, $height);

file_put_contents("media/imgname.jpg", $image);

function crop($image, $maxWidth, $height){
    ...how do I crop the image?
}
看一看通常是大多数PHP安装的一部分

通常,您会:

  • 使用imagecreatefromTYPE函数之一导入图像
  • 使用imagecreate($width,$height)创建空白缓冲区图像
  • 使用imagecopy()将所需的部分传输到缓冲区
  • 使用imageTYPE函数之一将缓冲区写入文件
  • 看一看通常是大多数PHP安装的一部分

    通常,您会:

  • 使用imagecreatefromTYPE函数之一导入图像
  • 使用imagecreate($width,$height)创建空白缓冲区图像
  • 使用imagecopy()将所需的部分传输到缓冲区
  • 使用imageTYPE函数之一将缓冲区写入文件

  • 如果您安装了GD库,请查看。如果您需要更多的解释和示例,请查看


    此外,还有很多方法可以帮助您。

    如果您安装了GD库,请查看。如果您需要更多的解释和示例,请查看


    此外,还有很多方法可以帮助您。

    如果您的服务器上未安装it GD,则可以使用Image Magick。

    如果您的服务器上未安装it GD,则可以使用Image Magick。

    查找以下使用GD库进行图像裁剪的代码:

    <?php
    function createThumb($upfile, $dstfile, $max_width, $max_height){
    
       $size = getimagesize($upfile);
    
       $width = $size[0];
    
       $height = $size[1];
    
    
    
       $x_ratio = $max_width / $width;
    
       $y_ratio = $max_height / $height;
    
       if( ($width <= $max_width) && ($height <= $max_height)) {
    
               $tn_width = $width;
    
               $tn_height = $height;
    
       } elseif (($x_ratio * $height) < $max_height) {
    
               $tn_height = ceil($x_ratio * $height);
    
               $tn_width = $max_width;
    
       } else {
    
               $tn_width = ceil($y_ratio * $width);
    
               $tn_height = $max_height;
    
       }
    
    
    
       if($size['mime'] == "image/jpeg"){
    
               $src = ImageCreateFromJpeg($upfile);
    
               $dst = ImageCreateTrueColor($tn_width, $tn_height);
    
               imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
    
               imageinterlace( $dst, true);
    
               ImageJpeg($dst, $dstfile, 100);
    
       } else if ($size['mime'] == "image/png"){
    
               $src = ImageCreateFrompng($upfile);
    
               $dst = ImageCreateTrueColor($tn_width, $tn_height);
    
               imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
    
               Imagepng($dst, $dstfile);
    
       } else {
    
               $src = ImageCreateFromGif($upfile);
    
               $dst = ImageCreateTrueColor($tn_width, $tn_height);
    
               imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
    
               imagegif($dst, $dstfile);
    
       }
    
    }
    
    //usage
    
    if(isset($_FILES['upload_Image']['name']) && $_FILES['upload_Image']['name']!=='') {
        $ext = substr($_FILES['upload_Image']['name'], strpos($_FILES['upload_Image']['name'],'.'), strlen($_FILES['upload_Image']['name'])-1); 
    
        $imgNormal = time().$ext;
    
        $normalDestination = "Photos/Orignal/" . $imgNormal;
    
        $httpRootLarge = "Photos/Large/" . $imgNormal;
    
        $httpRootSmall = "Photos/Small/" . $imgNormal;
    
        $httpRootThumb = "Photos/Thumb/" . $imgNormal;
    
        move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination);
    
    
    
        createThumb($normalDestination,$httpRootLarge,680,604); #For 604x604 Image  
    
        createThumb($normalDestination,$httpRootSmall,500,300); #For 500x300 Image  
    
        createThumb($normalDestination,$httpRootThumb,130,100); #For 130x100 Image
    
    }
    
    ?>
    

    使用GD库查找以下图像裁剪代码:

    <?php
    function createThumb($upfile, $dstfile, $max_width, $max_height){
    
       $size = getimagesize($upfile);
    
       $width = $size[0];
    
       $height = $size[1];
    
    
    
       $x_ratio = $max_width / $width;
    
       $y_ratio = $max_height / $height;
    
       if( ($width <= $max_width) && ($height <= $max_height)) {
    
               $tn_width = $width;
    
               $tn_height = $height;
    
       } elseif (($x_ratio * $height) < $max_height) {
    
               $tn_height = ceil($x_ratio * $height);
    
               $tn_width = $max_width;
    
       } else {
    
               $tn_width = ceil($y_ratio * $width);
    
               $tn_height = $max_height;
    
       }
    
    
    
       if($size['mime'] == "image/jpeg"){
    
               $src = ImageCreateFromJpeg($upfile);
    
               $dst = ImageCreateTrueColor($tn_width, $tn_height);
    
               imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
    
               imageinterlace( $dst, true);
    
               ImageJpeg($dst, $dstfile, 100);
    
       } else if ($size['mime'] == "image/png"){
    
               $src = ImageCreateFrompng($upfile);
    
               $dst = ImageCreateTrueColor($tn_width, $tn_height);
    
               imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
    
               Imagepng($dst, $dstfile);
    
       } else {
    
               $src = ImageCreateFromGif($upfile);
    
               $dst = ImageCreateTrueColor($tn_width, $tn_height);
    
               imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
    
               imagegif($dst, $dstfile);
    
       }
    
    }
    
    //usage
    
    if(isset($_FILES['upload_Image']['name']) && $_FILES['upload_Image']['name']!=='') {
        $ext = substr($_FILES['upload_Image']['name'], strpos($_FILES['upload_Image']['name'],'.'), strlen($_FILES['upload_Image']['name'])-1); 
    
        $imgNormal = time().$ext;
    
        $normalDestination = "Photos/Orignal/" . $imgNormal;
    
        $httpRootLarge = "Photos/Large/" . $imgNormal;
    
        $httpRootSmall = "Photos/Small/" . $imgNormal;
    
        $httpRootThumb = "Photos/Thumb/" . $imgNormal;
    
        move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination);
    
    
    
        createThumb($normalDestination,$httpRootLarge,680,604); #For 604x604 Image  
    
        createThumb($normalDestination,$httpRootSmall,500,300); #For 500x300 Image  
    
        createThumb($normalDestination,$httpRootThumb,130,100); #For 130x100 Image
    
    }
    
    ?>
    

    您可以使用timthumb进行croping,使用timtgumb可以动态调整图像大小

    检查这个


    以下是结果

    您可以使用timthumb进行croping,使用timtgumb可以动态调整图像大小

    检查这个


    下面是结果

    只需从Jquery中尝试使用JCROP插件

    裁剪图像的最佳插件之一

    在身体部位

    “id=“previews”name=“previews”onclick=“cropfun();"   />
    
    只需尝试从Jquery执行JCROP插件即可

    裁剪图像的最佳插件之一

    在身体部位

    “id=“previews”name=“previews”onclick=“cropfun();”/>
    
    他要的是php而不是JavaScript,尤其是jQuery解决方案,他要的是php而不是JavaScript,尤其是jQuery解决方案。
         <img  src="uploads/<?php  echo $image; ?>" id="previews" name="previews"  onclick="cropfun();"   />
         <input type="file" name="image22" id="image22" style="visibility:hidd"     >
        <input type="hidden" id="hh" name="hh" value="" />
         <input type="hidden" id="hhh" name="hhh" value="<?php  echo $image; ?>" />
            <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" name="submit" id="sub" value="submit" />
        </form>