簡體   English   中英

如何使用 php 水平和垂直翻轉圖像

[英]How to flip image horizontaly and vertically with php

我為此搜索了 web,但找不到我需要的東西。

我有一張圖片(在服務器內部或外部),我需要使用 php 水平或垂直翻轉圖像,並像這樣顯示:

<?
$img = $_GET['img'];
header('Content-type: image/png');
/*
do the flip work
*/
imagepng($img, NULL);
imagedestroy($tmp_img);
?>

我該怎么做? 謝謝你們。

如果您碰巧沒有 ImageMagick 可用,您也可以使用imagecopy系列函數來實現此目的。 這個例子

function ImageFlip ( $imgsrc, $mode )
{

    $width                        =    imagesx ( $imgsrc );
    $height                       =    imagesy ( $imgsrc );

    $src_x                        =    0;
    $src_y                        =    0;
    $src_width                    =    $width;
    $src_height                   =    $height;

    switch ( $mode )
    {

        case '1': //vertical
            $src_y                =    $height -1;
            $src_height           =    -$height;
        break;

        case '2': //horizontal
            $src_x                =    $width -1;
            $src_width            =    -$width;
        break;

        case '3': //both
            $src_x                =    $width -1;
            $src_y                =    $height -1;
            $src_width            =    -$width;
            $src_height           =    -$height;
        break;

        default:
            return $imgsrc;

    }

    $imgdest                    =    imagecreatetruecolor ( $width, $height );

    if ( imagecopyresampled ( $imgdest, $imgsrc, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height ) )
    {
        return $imgdest;
    }

    return $imgsrc;

}

使用ImageMagick以及flipImage()flopImage()方法,以下示例來自devzone.zend.com

<?php
try {
  // initialize object
  $image = new Gmagick();

  // read image file
  $image->readImage('gallery/original.jpg');

  // flip image vertically
  $image->flipImage();

  // write new image file
  $image->writeImage('gallery/new_1.jpg');

  // revert
  $image->flipImage();

  // flip image horizontally
  $image->flopImage();

  // write new image file
  $image->writeImage('gallery/new_2.jpg');

  // free resource handle
  $image->destroy();
} catch (Exception $e) {
  die ($e->getMessage());
}
?>

結果如下:

在此處輸入圖像描述

對於 PHP >= 5.5,您可以使用imageflip GD native function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM