php中使用curl发送请求和图片压缩

简单自用封装:在helper文件下新建curl_helper.php, 如果系统存在则命名前需加MY_,autoload中设置自动加载,则可以直接使用:echo send_get($url)

// 使用curl发送请求
function send_get($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);  // 返回数据,不直接输出,否则数据会追加一个布尔值
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求不验证证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // https请求不验证host
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

 图片压缩

/* 图片等比压缩函数
 * @param $maxwidth 最大宽度  
 * @param $maxheight 最大高度  
 * @param $name 压缩图片名  
 * @param $filetype 图片类型  
 */  
function resizeImage($orgin_file,$maxwidth,$maxheight,$name,$filetype)  
{  
    $im = '';  
    switch ($filetype){  
        case '.jpg':  
            $im=imagecreatefromjpeg($orgin_file);  
            break;  
        case '.png':  
            $im=imagecreatefrompng($orgin_file);  
            break;  
        case '.gif':  
            $im=imagecreatefromgif($orgin_file);  
            break;  
        default:  
            $im=imagecreatefromstring(file_get_contents($orgin_file));  
    }  
    $pic_width = imagesx($im);  
    $pic_height = imagesy($im);  
  
    if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))  
    {  
        if($maxwidth && $pic_width>$maxwidth)  
        {  
            $widthratio = $maxwidth/$pic_width;  
            $resizewidth_tag = true;  
        }  
  
        if($maxheight && $pic_height>$maxheight)  
        {  
            $heightratio = $maxheight/$pic_height;  
            $resizeheight_tag = true;  
        }  
  
        if($resizewidth_tag && $resizeheight_tag)  
        {  
            if($widthratio<$heightratio)  
                $ratio = $widthratio;  
            else  
                $ratio = $heightratio;  
        }  
  
        if($resizewidth_tag && !$resizeheight_tag)  
            $ratio = $widthratio;  
        if($resizeheight_tag && !$resizewidth_tag)  
            $ratio = $heightratio;  
  
        $newwidth = $pic_width * $ratio;  
        $newheight = $pic_height * $ratio;  
  
        if(function_exists("imagecopyresampled"))  
        {  
            $newim = imagecreatetruecolor($newwidth,$newheight);//PHP系统函数  
            imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP系统函数  
        }  
        else  
        {  
            $newim = imagecreate($newwidth,$newheight);  
            imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);  
        }  
  
        $name = $name.$filetype;  
        switch ($filetype){  
            case '.jpg':  
                imagejpeg($newim,$name);  
                break;  
            case '.png':  
                imagepng($newim,$name);  
                break;  
            case '.gif':  
                imagegif($newim,$name);  
                break;  
            default:  
                imagegd($newim,$name);  
        }  
  
        imagedestroy($newim);  
    }  
    else  
    {  
        $name = $name.$filetype;  
        switch ($filetype){  
            case '.jpg':  
                imagejpeg($im,$name);  
                break;  
            case '.png':  
                imagepng($im,$name);  
                break;  
            case '.gif':  
                imagegif($im,$name);  
                break;  
            default:  
                imagegd($im,$name);  
        }  
    }  
}
原文地址:https://www.cnblogs.com/maoriaty/p/9093463.html