先用PHP读取整个页面文件,然后把取到的内容,用正则表达式~~~去取数据,把所有有img标签的内容都读出来,不过这个有两个问题不可避免,第一就是因为页面当中图片的显示,不一定全是img标签,有些是通过CSS样式去控制显示,第二个就是页面当中取到的图片不一定是所谓的缩略图,而是页面当中所有的图片都取出来了
知是不是只需要抓取页面中的图像,还是说输入一个网址,然后把这个网址生成的页面做成一个缩略图,
如果是第一个,那很容易,上面已经说了如何做
如果是第二个,现在网上有类似的解决方法,具体的方法我记得不清楚了,貌似需要调用第三方的工具,LZ可以百度一下 "PHP 生成网站图"
$url = "自己找个腾讯拍拍产品详细页替换就好了";
$content = file_get_contents($url);
preg_match_all('/<form id="itemForm" name="itemForm".*?>[\s\S]*<h1>(.*?)<\/h1>[\s\S]*<em id="commodityCurrentPrice" .*?>(.*?)<\/em>[\s\S]*picList:\[\["(.*?)"/',$content,$arr);
*仅支持三种图片的缩略图:gif,jpg,png
* $sourcefile 源图像的路径及文件名
* $dest_x 要生成的图像宽
* $dest_y 要生成的图像的高
* $targetfile 要生成的图像路径及文件名
* $jpegqual 图像的质量(0-100越小图片越小同时影响图片模糊)
*/
function resizeToFile2 ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual)
{
$picsize=getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize[1];
//[-------------------------------修改过的地方
$arr=explode(".",$sourcefile);
$ext="";
if(isset($arr[count($arr)-1]))
{
$ext=$arr[count($arr)-1];
$ext=strtolower($ext);
}
if($ext=="jpg" or $ext=="jpeg"){
$source_id = imageCreateFromJPEG("$sourcefile");
}elseif($ext=="gif"){
$source_id =imagecreatefromgif("$sourcefile");
}elseif($ext=="png"){
$source_id=imagecreatefrompng("$sourcefile");
}
$width=imagesx($source_id);
$height=imagesy($source_id);
//---------------------------------]
$target_id=imagecreatetruecolor($dest_x, $dest_y);
$target_pic=imagecopyresampled($target_id,$source_id,
0,0,0,0,
$dest_x,$dest_y,
$source_x,$source_y);
imagejpeg ($target_id,"$targetfile",$jpegqual);
return true;}