简体   繁体   English

使用PHP裁剪图像

[英]Crop image using php

I want to crop an image. 我想裁剪图像。 Is it possible to do like this. 有可能这样做吗? And if yes, how would the crop() function look like? 如果是的话, crop()函数的外观如何?

$imgURL is always a .jpg image. $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?
}

If you have the GD library installed, taked a look at the functions available to you . 如果您已安装GD库,请查看可用的功能 If you want more explanation and an example, take a look at this blog . 如果您需要更多说明和示例,请访问此博客

Also, there's plenty of SO posts to help you along. 另外,有很多SO帖子可以帮助您。

Take a look at the gd library that's usually part of most PHP installs. 看一下gd库 ,它通常是大多数PHP安装中的一部分。

Typically, you'd: 通常,您会:

  1. import the image using one of the imagecreatefromTYPE functions 使用imagecreatefromTYPE函数之一导入图像
  2. use imagecreate($width,$height) to make a blank buffer image 使用imagecreate($ width,$ height)制作空白缓冲区图像
  3. use imagecopy() to transfer the portion you want to the buffer 使用imagecopy()将想要的部分转移到缓冲区
  4. use one of the imageTYPE functions to write the buffer out to a file. 使用imageTYPE函数之一将缓冲区写出到文件中。

you can use timthumb for croping , using timtgumb you can resize image on fly.... 您可以使用timthumb进行裁剪,使用timtgumb可以即时调整图像大小。

check this http://www.wprecipes.com/how-to-resize-images-on-the-fly 检查此http://www.wprecipes.com/how-to-resize-images-on-the-fly

here is the result http://joedesigns.com/resizing2/example.php 这是结果http://joedesigns.com/resizing2/example.php

如果服务器上未安装GD,则可能可以使用的另一个选项是Image Magick。

Just try doing JCROP plugin from Jquery 只需尝试从Jquery做JCROP插件

One of the best plugin for cropping images 裁剪图像的最佳插件之一

deepliquid.com----download JCROP deepliquid.com ----下载JCROP

 function cropfun(){         
            $('#previews').Jcrop({ 
                aspectRatio: 3,
                minSize:[300,100] ,
                boxWidth: 450, boxHeight: 400,
                bgFade:true,
                bgOpacity: .5,
                setSelect: [ 60, 70, 600, 330 ],
                onSelect: updateCoords
            });

}

 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('Select where you want to Crop.');
            return false;
        };

In the body part 在身体部位

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

Find below code for image crop using GD library: 查找以下使用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

}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM