简体   繁体   English

PHP中的图像裁剪

[英]Image cropping in php

So I am trying to write a little script to crop a users image. 因此,我试图编写一个小脚本来裁剪用户图像。 I would send some information (width, height, alignment properties and image url) to the script and it should return the image cropped. 我会向脚本发送一些信息(宽度,高度,对齐属性和图像url),并且它应该返回裁剪的图像。 However, it's not working... Just an "image not found" symbol :/ Here is my script, any thoughts? 但是,它不起作用...只是一个“未找到图像”符号:/这是我的脚本,有什么想法吗?

<?php
session_start();
header('Content-type: image/jpeg');
$w=$_GET['w'];
$h=$_GET['h'];
$x=$_GET['x'];
$y=$_GET['y'];
$filename="http://www.domain.com/".$_GET['src'];
$file_ext = substr($filename, strrpos($filename, ".") + 1);
$ext='';

if($file_ext=='jpg')
{
    $image = imagecreatefromjpeg($filename); 
}
else if ($file_ext=='gif')
{
    $image = imagecreatefromgif($filename); 
}
else if ($file_ext=='png')
{
    $image = imagecreatefrompng($filename); 
} 

$crop = imagecreatetruecolor($w,$h);
imagecopy($crop, $image, 0, 0, $x, $y, $w, $h);
imagejpeg($crop);
?>

Edit: Looks like this is the error: Fatal error: Call to undefined function imagecreatetruecolor() in domainpath/crop.php on line 24 Is there anything I need to do to load this function? 编辑:看起来像是错误:致命错误:在第24行的domainpath / crop.php中调用未定义的函数imagecreatetruecolor(),我需要做些什么来加载此函数?

According to your comment about the error, imagecreatetruecolor() is indeed a function, but only if you have the GD library loaded. 根据您对错误的评论, imagecreatetruecolor()确实是一个函数,但前提是您已加载GD库。

Make sure you have a compatible version of GD accessible to your PHP installation, and don't forget to restart your web server if you have just added it. 确保您的PHP安装具有可访问的GD兼容版本,并且如果刚添加了Web服务器,请不要忘记重新启动它。

Using ImageMagick , try this; 使用ImageMagick ,尝试这个;

<?php
function resize_image($file, $w, $h, $crop=FALSE) {
    $img = new Imagick($file);
    if ($crop) {
        $img->cropThumbnailImage($w, $h);
    } else {
        $img->thumbnailImage($w, $h, TRUE);
    }

    return $img;
}
resize_image(‘/path/to/some/image.jpg’, 150, 150);

I will recommend u use imagemagick instead of GD / GD2 lib. 我建议您使用imagemagick而不是GD / GD2 lib。 If u looking for quality, imagemagick is too good for the same. 如果您追求质量,那么imagemagick太适合了。 There is this link which also gives compares on the same http://www.rubblewebs.co.uk/imagemagick/compair.php 有此链接也可以在同一http://www.rubblewebs.co.uk/imagemagick/compair.php上进行比较

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

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