Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- configure.ac | 2 ++ examples/index.php | 6 ++--- examples/libvirt.php | 68 ++++++++++++++++++++++++------------------------ libvirt-php.spec.in | 1 + m4/virt-php-extension.m4 | 35 +++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 m4/virt-php-extension.m4 diff --git a/configure.ac b/configure.ac index ed0d555..a82f7e6 100644 --- a/configure.ac +++ b/configure.ac @@ -164,6 +164,8 @@ AC_SUBST(PHP_LDFLAGS) LIBVIRT_CHECK_PHP_EXTENSIONDIR LIBVIRT_CHECK_PHP_CONFDIR +LIBVIRT_CHECK_PHP_EXTENSION([imagick]) + OS=`uname` if test "$OS" = "Darwin"; then WL=-Wl, diff --git a/examples/index.php b/examples/index.php index bdeb506..1ba8193 100644 --- a/examples/index.php +++ b/examples/index.php @@ -14,13 +14,13 @@ if (array_key_exists('width', $_GET) && $_GET['width']) $tmp = $lv->domain_get_screenshot_thumbnail($_GET['uuid'], $_GET['width']); else - $tmp = $lv->domain_get_screenshot($_GET['uuid']); + $tmp = $lv->domain_get_screenshot($_GET['uuid'], 0); if (!$tmp) { echo $lv->get_last_error().'<br/>'; } else { - Header('Content-Type: image/png'); - die($tmp); + Header('Content-Type: ' . $tmp['mime']); + die($tmp['data']); } } ?> diff --git a/examples/libvirt.php b/examples/libvirt.php index 32d8314..f65c50e 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -61,50 +61,50 @@ class Libvirt { return ($tmp) ? $tmp : $this->_set_last_error(); } - function domain_get_screenshot($domain) { + function domain_get_screenshot($domain, $convert = 1) { $dom = $this->get_domain_object($domain); - $tmp = libvirt_domain_get_screenshot($dom, $this->get_hostname() ); - return ($tmp) ? $tmp : $this->_set_last_error(); + $tmp = libvirt_domain_get_screenshot_api($dom); + if ($tmp == false) + return $this->_set_last_error(); + + $mime = $tmp['mime']; + + if ($convert && $tmp['mime'] != "image/png") { + $image = new Imagick(); + $image->readImage($tmp['file']); + $image->setImageFormat("png"); + $data = $image->getImageBlob(); + $mime = "image/png"; + } else { + $fp = fopen($tmp['file'], "rb"); + $data = fread($fp, filesize($tmp['file'])); + fclose($fp); + } + unlink($tmp['file']); + unset($tmp['file']); + $tmp['data'] = $data; + $tmp['mime'] = $mime; + + return $tmp; } function domain_get_screenshot_thumbnail($domain, $w=120) { $screen = $this->domain_get_screenshot($domain); - $imgFile = tempnam("/tmp", "libvirt-php-tmp-resize-XXXXXX");; - if ($screen) { - $fp = fopen($imgFile, "wb"); - fwrite($fp, $screen); - fclose($fp); - } + if (!$screen) + return false; - if (file_exists($imgFile) && $screen) { - list($width, $height) = getimagesize($imgFile); - $h = ($height / $width) * $w; - } else { - $w = $h = 1; - //$h = $w * (3 / 4.5); - } + $image = new Imagick(); + $image->readImageBlob($screen['data']); + $origW = $image->getImageWidth(); + $origH = $image->getImageHeight(); + $h = ($w / $origW) * $origH; + $image->resizeImage($w, $h, 0, 0); - $new = imagecreatetruecolor($w, $h); - if ($screen) { - $img = imagecreatefrompng($imgFile); - imagecopyresampled($new,$img,0,0,0,0, $w,$h,$width,$height); - imagedestroy($img); - } else { - $c = imagecolorallocate($new, 255, 255, 255); - imagefill($new, 0, 0, $c); - } + $screen['data'] = $image->getImageBlob(); - imagepng($new, $imgFile); - imagedestroy($new); - - $fp = fopen($imgFile, "rb"); - $data = fread($fp, filesize($imgFile)); - fclose($fp); - - unlink($imgFile); - return $data; + return $screen; } function domain_get_screen_dimensions($domain) { diff --git a/libvirt-php.spec.in b/libvirt-php.spec.in index 2aae1d5..8cf6144 100644 --- a/libvirt-php.spec.in +++ b/libvirt-php.spec.in @@ -27,6 +27,7 @@ BuildRequires: php-devel BuildRequires: libvirt-devel >= %{req_libvirt_version} BuildRequires: libxml2-devel BuildRequires: libxslt +BuildRequires: php-pecl-imagick %if 0%{?suse_version} BuildRequires: xhtml-dtd %else diff --git a/m4/virt-php-extension.m4 b/m4/virt-php-extension.m4 new file mode 100644 index 0000000..9040eb1 --- /dev/null +++ b/m4/virt-php-extension.m4 @@ -0,0 +1,35 @@ +dnl The libvirt-php.so config +dnl +dnl Copyright (C) 2016 Red Hat, Inc. +dnl +dnl This library is free software; you can redistribute it and/or +dnl modify it under the terms of the GNU Lesser General Public +dnl License as published by the Free Software Foundation; either +dnl version 2.1 of the License, or (at your option) any later version. +dnl +dnl This library is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +dnl Lesser General Public License for more details. +dnl +dnl You should have received a copy of the GNU Lesser General Public +dnl License along with this library. If not, see +dnl <http://www.gnu.org/licenses/>. +dnl + +dnl +dnl Check whether php module exists +dnl +dnl LIBVIRT_CHECK_PHP_EXTENSION([EXTENSION]) +dnl +AC_DEFUN([LIBVIRT_CHECK_PHP_EXTENSION],[ + AC_MSG_CHECKING([for php module $1]) + + module="$(php -m | grep $1)" + + if test "x$module" = "x"; then + AC_MSG_ERROR([php module $1 not found]) + else + AC_MSG_RESULT([found]) + fi +]) -- 2.8.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list