0

我想裁剪图像。有没有可能这样做。如果是的话,这个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?
}
4

6 回答 6

3

如果您安装了 GD 库,请查看可供您使用的功能。如果您需要更多解释和示例,请查看此博客

此外,还有很多SO 帖子可以帮助您。

于 2009-09-03T10:04:42.533 回答
2

看看通常是大多数 PHP 安装的一部分的gd 库

通常,您会:

  1. 使用 imagecreatefromTYPE 函数之一导入图像
  2. 使用 imagecreate($width,$height) 制作空白缓冲区图像
  3. 使用 imagecopy() 将您想要的部分传输到缓冲区
  4. 使用其中一个 imageTYPE 函数将缓冲区写入文件。
于 2009-09-03T10:02:53.940 回答
1

您可以使用 timthumb 进行裁剪,使用 timtgumb 您可以即时调整图像大小....

检查这个http://www.wprecipes.com/how-to-resize-images-on-the-fly

这是结果http://joedesigns.com/resizing2/example.php

于 2012-03-12T12:36:48.423 回答
0

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

于 2009-09-03T10:44:22.703 回答
0

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

}

?>
于 2011-05-30T11:25:56.500 回答
0

试着从 Jquery 做 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;
        };

在身体部位

     <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>
于 2013-06-06T09:38:09.570 回答