GD/Imagick 不保存我的圖像 (GD/Imagick doesn't save my image)


問題描述

GD/Imagick 不保存我的圖像 (GD/Imagick doesn't save my image)

I want to send an image from android and save it trought a php code in my server. Because I need to protect my server I need to reprocess the image so I have to use GD or Imagick. Image is sent encoded in base64. In the php file is decoded and if I use:

$img=base64_decode($img);
$file = fopen('test.jpg', 'wb');
fwrite($file, $img);
fclose($file);

image is saved in the server. If I use Imagick or GD my image isn't saved and the php files returns nothing, even if I insert an echo after it. Any help? In GD I was using:

$img=base64_decode($img);
$imageinfo = getimagesize($img);
if($imageinfo['mime'] == 'image/gif'{
    $img = @imagecreatefromjpeg($img);
    imagejpeg($img, ""testt.jpg", 80);
}

and in Imagick:

$img=base64_decode($img);
$img = imagick($img); //neither if I use a path of an image saved in my server 
                      //works
$mime = mime_content_type($img);
    switch ($mime):
        case 'image/jpeg':
            $ext = '.jpg';
            break;
    endswitch;

$img‑>stripImage();
$img‑>writeImage(""testt".$ext);
$img‑>clear();
$img‑>destroy();

‑‑‑‑‑

參考解法

方法 1:

imagick only works on files, you can't give a handle to it unfortunately, so:

$img=base64_decode($img);
$filename = tempnam(sys_get_temp_dir(), 'tempimage_'); 
$file = fopen($filename, 'wb');
fwrite($file, $img);
fclose($file);

$img = imagick($filename);  
$mime = mime_content_type($img);
    switch ($mime):
        case 'image/jpeg':
            $ext = '.jpg';
            break;
   endswitch;

$img‑>stripImage();
$img‑>writeImage("testt".$ext);
$img‑>clear();
$img‑>destroy();

This, in theory, should work.

(by Learning from mastersAadaam)

參考文件

  1. GD/Imagick doesn't save my image (CC BY‑SA 3.0/4.0)

#imagick #Android #PHP #gd






相關問題

Imagick 安裝錯誤 - 類未定義 (Imagick Installation Errors - Class Undefined)

GD/Imagick 不保存我的圖像 (GD/Imagick doesn't save my image)

Imagick 使用不同的目錄和數據庫 PDF 來縮略圖 JPG (Imagick using different directory and database PDF to thumbnail JPG)

如何在不損失分辨率和質量的情況下減小圖像尺寸(高度、寬度) (How to decrease image size(height, width) without losing its resolution and quality)

Đang cố gắng để tưởng tượng chạy trên PHP 5.4.3 ở Windows x64 (Trying to get imagick running on PHP 5.4.3 at Windows x64)

WampServer 和 Imagemagick,無法識別 imagick php 模塊 (WampServer and Imagemagick, imagick php module not recogized)

PHP Imagick:如何將自定義圖像屬性保存到文件 (PHP Imagick: How to save custom image property to file)

PHP Imagick - 顯示多圖像 TIFF 的第一片 (PHP Imagick - display first slice of mutli image TIFF)

可以加快 php/imagick 中的圖像加載速度 (Possible to speed up image loading in php/imagick)

PHP imagick 相當於 -define png:color-type=6 (PHP imagick equivalent of -define png:color-type=6)

php Imagick::levelImage 使用 (php Imagick::levelImage usage)

imagick:無法查詢字體度量 (Imagick: Failed to query the font metrics)







留言討論