【问题标题】:Image Skimming with php & gd;使用 php & gd; 进行图像浏览
【发布时间】:2010-09-14 17:17:35
【问题描述】:

我已经看到了大量使用 php 和 gd 库裁剪图像的示例,但是我从未见过任何关于略读或刮削图像的帖子。我的意思是说你有数码相机的照片,上面有日期。它总是在图片上的一个固定位置。那么我该怎么做呢?我遇到的所有示例都涉及保持纵横比,我只希望底部的那些 75px 左右。这怎么做最简单?我发现这个例子有点启发性! imagecopyresampled in PHP, can someone explain it?

【问题讨论】:

    标签: php height gd crop


    【解决方案1】:

    要创建您需要的内容,请阅读此页面:http://us.php.net/imagecopy

    您可以使用类似的方法,只需更改 $left 和 $top 以对应图像上日期戳的位置。

    <?php
    // Original image
    $filename = 'someimage.jpg';
    
    // Get dimensions of the original image
    list($current_width, $current_height) = getimagesize($filename);
    
    // The x and y coordinates on the original image where we
    // will begin cropping the image
    $left = 50;
    $top = 50;
    
    // This will be the final size of the image (e.g. how many pixels
    // left and down we will be going)
    $crop_width = 200;
    $crop_height = 200;
    
    // Resample the image
    $canvas = imagecreatetruecolor($crop_width, $crop_height);
    $current_image = imagecreatefromjpeg($filename);
    imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
    imagejpeg($canvas, $filename, 100);
    ?>
    

    类似的例子是:

    实现“裁剪”功能的基本方法:给定图像 (src)、偏移量 (x, y) 和大小 (w, h)。

    crop.php:

    <?php
    $w=$_GET['w'];
    $h=isset($_GET['h'])?$_GET['h']:$w;    // h est facultatif, =w par défaut
    $x=isset($_GET['x'])?$_GET['x']:0;    // x est facultatif, 0 par défaut
    $y=isset($_GET['y'])?$_GET['y']:0;    // y est facultatif, 0 par défaut
    $filename=$_GET['src'];
    header('Content-type: image/jpg');
    header('Content-Disposition: attachment; filename='.$src);
    $image = imagecreatefromjpeg($filename); 
    $crop = imagecreatetruecolor($w,$h);
    imagecopy ( $crop, $image, 0, 0, $x, $y, $w, $h );
    imagejpeg($crop);
    ?>
    

    这样称呼它:

    <img src="crop.php?x=10&y=20&w=30&h=40&src=photo.jpg">
    

    【讨论】:

    • 好的日期和时间戳总是在右下角所以在你的例子中我应该这样做。$skim = 75; $左= 0; $顶部 = 0; (从左上角/前 0 位置开始)然后 $crop_width = imagesx($file); $crop_height = (imagesy($file) - $skim);就像我之前说的那样,我并没有尝试以 200x200 w/h 的比例对其进行缩略图,也没有尝试保持任何方面,因为从整个图像中略读 75px 真的甚至看不到
    • Imagecopy 不起作用,它所做的只是在应该被裁剪的区域上用黑色填充相同的图像。但这让我更仔细地查看了我提供的链接。
    猜你喜欢
    • 2014-06-03
    • 2012-11-13
    • 2013-04-06
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2012-08-12
    相关资源
    最近更新 更多