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);