Php 获取图像url,应用转换,保存到文件夹
我正在尝试制作一个脚本来获取图像列表(url),我想应用函数findexts(),然后获取图像的高度和宽度,并将其保存到文件夹中。我在这里缩短了我的代码。。。我无法获取要处理的文件中的图像Php 获取图像url,应用转换,保存到文件夹,php,Php,我正在尝试制作一个脚本来获取图像列表(url),我想应用函数findexts(),然后获取图像的高度和宽度,并将其保存到文件夹中。我在这里缩短了我的代码。。。我无法获取要处理的文件中的图像 function findexts ($filename) { $filename = strtolower($filename) ; $exts = preg_split("%[/\\\\.]%", $filename); $n = count($exts)-1; $exts = $exts[$n]
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>";
}
foreach($matches as$match){
//$match[1]=类似http://www.....jpg
//$match[2]=我的博客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\u exec($ch);
卷曲关闭($ch);
如果(mime内容类型($data)=“图像/jpeg”)
{$img=imagecreatefromjpeg($data);}
elseif(mime内容类型($data)==“图像/png”)
{$img=imagecreatefrompng($data);}
elseif(mime内容类型($data)==“图像/gif”)
{$img=imagecreatefromformgif($data);}
//创建具有适当扩展名的随机文件名。
$ext=findexts($img);
$ran=rand();
$ran2=$ran.“;
$sourcefilename=$ran2.$ext;
//获取原始图像的宽度和高度
$width=imagesx($img);
$height=imagesy($img);
echo“width=“.$width.”height=“.$height.”filename=“.$sourcefilename.”
“;
}
我得到:
警告:mime内容类型[函数.mime内容类型]:无法打开流:
及
警告:imagesy()希望参数1是资源
显然我的$img是错的 您可以使用检查图像的延伸、高度和宽度,请检查:
$imagen="lol.jpg"; //an jpg image 300x100
$condicion = GetImageSize($imagen);
echo $condicion[0]."<br>";
echo $condicion[1]."<br>";
echo $condicion[2]."<br>";
$condicion[2]
是图像的数字扩展:
1=GIF,2=JPG,3=PNG,4=SWF,5=PSD,6=BMP,7=TIFF(英特尔字节顺序),
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>";
$match[1]=”http://s5.mangareader.net/cover/the-breaker-new-waves/the-breaker-new-waves-m0.jpg";
$data_image=GetImageSize($match[1]);
如果($data_image[2]==1){//gif
$img=imagecreatefromformgif($match[1]);
$ext=“.gif”;
}
如果($data_image[2]==2){//jpg
$img=imagecreatefromjpeg($match[1]);
$ext=“.jpg”;
}
如果($data_image[2]==3){//png
$img=imagecreatefrompng($match[1]);
$ext=“.png”;
}
//创建具有适当扩展名的随机文件名。
$ran=rand();
$sourcefilename=$ran.$ext;
//获取原始图像的宽度和高度
$width=$data_图像[0];
$height=$data_image[1];
echo“width=“.$width.”height=“.$height.”filename=“.$sourcefilename.”
“;
尝试使用一个简单的图像(最好是.jpg
),名称中只有拉丁字符。。
$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>";