拍摄图像网址,应用转换,保存到文件夹

时间:2014-02-15 00:43:09

标签: php

我正在尝试制作一个脚本来获取图像列表(url),我想应用函数findexts(),然后获取图像的高度和宽度并将其保存到文件夹中。我在这里缩短了我的代码...我无法将图像放在要处理的文件中。

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = preg_split("%[/\\\\.]%", $filename);
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

//我有更多代码来获取我的数据..然后

foreach ($matches as $match) {

    // $match[1] = a URL like http://www.....jpg
    // $match[2] = my blog_id

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $match[1]); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);

    if(mime_content_type ($data) == "image/jpeg")
    { $img = imagecreatefromjpeg($data); }
    elseif(mime_content_type ($data) == "image/png")
    { $img = imagecreatefrompng($data); }
    elseif(mime_content_type ($data) == "image/gif")
    { $img = imagecreatefromgif($data); }



    //create random file name with proper extention.
    $ext = findexts ($img) ; 
    $ran = rand () ;
    $ran2 = $ran.".";
    $sourcefilename = $ran2.$ext; 


    //get width and height of original image
    $width = imagesx($img); 
    $height = imagesy($img);

    echo "width=".$width." height=".$height." filename=".$sourcefilename."<br>";

}

我得到:     警告:mime_content_type(ÿØÿà)[function.mime-content-type]:无法打开流:

和     警告:imagesy()希望参数1为资源

显然我的$ img错了......

1 个答案:

答案 0 :(得分:1)

您可以使用GetImageSize检查图片的扩展名,高度和宽度,请检查:

$imagen="lol.jpg";   //an jpg image 300x100
$condicion = GetImageSize($imagen); 
echo $condicion[0]."<br>";
echo $condicion[1]."<br>";
echo $condicion[2]."<br>";

将回应:

300
100
2 -> jpg

$condicion[2]是图片的扩展名:

1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(orden de bytes intel), 8 = TIFF(orden de bytes motorola),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC, 14 = IFF,15 = WBMP,16 = XBM。

这可能会对你有所帮助

$match[1]="http://s5.mangareader.net/cover/the-breaker-new-waves/the-breaker-new-waves-m0.jpg";

    $data_image = GetImageSize($match[1]); 

        if($data_image[2] == 1){ // gif
        $img = imagecreatefromgif("$match[1]");
        $ext=".gif";
        }
        if($data_image[2] == 2){ //jpg
        $img = imagecreatefromjpeg("$match[1]");
        $ext=".jpg";
        }
        if($data_image[2] == 3){ //  png
        $img = imagecreatefrompng("$match[1]"); 
        $ext=".png";
        }

    //create random file name with proper extention.
        $ran = rand () ;
        $sourcefilename = $ran.$ext; 

    //get width and height of original image
    $width = $data_image[0]; 
    $height = $data_image[1];

    echo "width=".$width." height=".$height." filename=".$sourcefilename."<br>";