php远程图片下载、图片格式转化、MySQL字段值部分修改

编程

/**

* 搜索文章的所有图片

* @param $content

* @return mixed

*/

private function allImgInfo($content){

/* $pattern = "/<[img|IMG].*?src=["|"](.*?(?:[.gif | .jpg | .jpeg | .bmp | .png]))["|"].*?[/]?>/";*/

$pattern = "/<img[^>]*srcs*=s*[" | "]?s*([^>""s]*)(">|"/>)/i";

preg_match_all($pattern, $content, $match);

return $match;

}

/**

* 下载远程图片

* @param $imgUrl

* @param $imgType

*/

private function downImage($imgUrl, $imgType){

// if($imgType == "jpeg"){$imgType = "jpg";}

$des = Yii::$app->basePath."/web/OssUpload/article/";

if(!is_dir($des)) mkdir($des,0777,true);

$randomFileName = Yii::$app->user->identity->id . "-".time() . mt_rand(000000,99999) . "." . basename($imgType);

$in= fopen($imgUrl, "rb");

$out= fopen($des.$randomFileName, "wb");

while ($chunk = fread($in,8192)) {

fwrite($out, $chunk, 8192);

}

fclose($in);

fclose($out);

// 如果格式为 jpeg 则转化为 jpg

if($imgType == "jpeg"){

$tempName = str_replace(".jpeg",".jpg",$randomFileName);

copy($des.$randomFileName,$des.$tempName);

unlink($des.$randomFileName);

$randomFileName = $tempName;

// print_r($des.$randomFileName);

}

$saveUrl = "cms/article/" .date("Y",time())."/".date("m",time()). "/" . $randomFileName;

$fileAbsolutePath = $des . $randomFileName;

$format = "";//?x-oss-process=image/resize,m_lfit,h_500,w_400

$aliOss = new AliOss();

$imagePath = $aliOss->uploadFile($fileAbsolutePath, $saveUrl, $format);

if (is_string($imagePath)) {

unlink($fileAbsolutePath);

} else {

$imagePath = strstr($fileAbsolutePath, "/web");

}

return $imagePath;

}

 

sql语法: UPDATE 表名 SET 字段名=replace(字段名, ‘被替换字符串’, "用来替换的字符串") ;

sql: UPDATE `member` SET `phone`=replace(`phone`, """, "") ;

以上是 php远程图片下载、图片格式转化、MySQL字段值部分修改 的全部内容, 来源链接: utcz.com/z/517293.html

回到顶部