2017-02-21 44 views
0

我正在寻找像https://something.com/my/render_thumb.php?size=small这样的Iamge URL,因为如果它看起来是透明的,因为如果它是部分透明的,那么我会认为它是完全透明的,然后使用不同的图像在我的代码。我试图创建一个读取像这样的url的函数,但它抱怨$ url是字符串或类似的东西。关于如何快速检查图片网址的任何想法?检查一个图像的URL是否透明与PHP

// --pseudo php code (doesn't work) -- 
function check_transparent($url) { 

    $ch = curl_init ($url); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
    $raw = curl_exec($ch); 
    curl_close ($ch); 

    $img = ImageCreateFromJpeg($raw); 
    // We run the image pixel by pixel and as soon as we find a transparent pixel we stop and return true. 
    for($i = 0; $i < 10; $i++) { 
     for($j = 0; $j < 10; $j++) { 
      $rgba = imagecolorat($img, $i, $j); 
      if(($rgba & 0x7F000000) >> 24) { 
       return true; 
      } 
     } 
    } 

    // If we dont find any pixel the function will return false. 
    return false; 
} 

回答

2

你的问题涉及到如何使用ImageCreateFromJpeg功能。

Check out PHP docs你会注意到该函数需要一个有效的JPEG路径或URL,而不是它的原始值。因此,这应该是在你的函数的第一行:

$img = ImageCreateFromJpeg($url); 
0

感谢,所以大概是这样的可能做的伎俩....会给它一个镜头...。

function getImageType($image_path) { 
$typeString = ''; 
$typeInt = exif_imagetype($image_path); 
switch ($typeInt) { 
    case IMAGETYPE_GIF: 
    case IMG_GIF: 
     $typeString = 'image/gif'; 
     break; 
    case IMG_JPG: 
    case IMAGETYPE_JPEG: 
    case IMG_JPEG: 
     $typeString = 'image/jpg'; 
     break; 
    case IMAGETYPE_PNG: 
    case IMG_PNG: 
     $typeString = 'image/png'; 
     break; 
    default: 
     $typeString = 'other ('.$typeInt.')'; 
} 
return $typeString; 
} 


function check_transparent($url) { 

$imgType = ''; 
$imgType = getImageType($url); 

if ($imgType == 'image/jpg') { 
    $img = ImageCreateFromJpeg($url); 
} else if ($imgType == 'image/png') { 
    $img = ImageCreateFromPng($url); 
} else { 
    return false; 
} 

// We run the image pixel by pixel and as soon as we find a transparent pixel we stop and return true. 
for($i = 0; $i < 200; $i++) { 
    for($j = 0; $j < 200; $j++) { 
     $rgba = imagecolorat($img, $i, $j); 
     if(($rgba & 0x7F000000) >> 24) { 
      return true; 
     } 
    } 
} 

// If we dont find any pixel the function will return false. 
return false; 
} 

实施例:

$url = "https://example.com/images/thumb_maker.php?size=200x200"; 

echo check_transparent($url);