• php保存远程图片改变尺寸,JPG转PNG,裁剪圆形


    //$radius设置圆角弧度

    function getyuan(){
    $radius = 20;
    $img = imagecreatetruecolor($radius, $radius);
    $bgcolor = imagecolorallocate($img, 255, 255, 255);
    $fgcolor = imagecolorallocate($img, 0, 0, 0);
    imagefill($img, 0, 0, $bgcolor);
    imagefilledarc($img, $radius, $radius, $radius*2, $radius*2,
    180, 270, $fgcolor, IMG_ARC_PIE);
    imagecolortransparent($img, $fgcolor);
    return $img;
    }

    function put_file_from_url_content($url, $saveName, $path) {
    // 设置运行时间为无限制
    set_time_limit ( 0 );

    $url = trim ( $url );
    $curl = curl_init ();
    // 设置你需要抓取的URL
    curl_setopt ( $curl, CURLOPT_URL, $url );
    // 设置header
    curl_setopt ( $curl, CURLOPT_HEADER, 0 );
    // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
    curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
    // 运行cURL,请求网页
    $file = curl_exec ( $curl );
    // 关闭URL请求
    curl_close ( $curl );
    // 将文件写入获得的数据
    $filename = $path . $saveName;
    $write = @fopen ( $filename, "w" );
    if ($write == false) {
    return false;
    }
    if (fwrite ( $write, $file ) == false) {
    return false;
    }
    if (fclose ( $write ) == false) {
    return false;
    }
    }

    $url=“www.xxx.com”;

    $file2=“headimg/xxx.png”;//第一次保存后路径

    put_file_from_url_content($url, "xxx.png", "headimg/");//远程图片地址,保存图片的名字,保存文件夹
    list($width, $height) = getimagesize($file2);
    $new_width = 40;
    $new_height = 40;
    $image_wp = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($file2);
    imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagejpeg($image_wp, $file2, 75);
    imagedestroy($image_wp);
    /**
    * 生成圆形图
    */

    // load the source image
    $src_image = imagecreatefromjpeg($file2);
    if ($src_image === false) {
    die('Sorry, can/t load the image');
    }
    $image_width = imagesx($src_image);
    $image_height = imagesy($src_image);
    if($image_width < $image_height){
    $imageSize = $image_width;
    }else{
    $imageSize = $image_height;
    }


    // create a new image, with src_width, src_height, and fill it with transparent color
    $image = imagecreatetruecolor($imageSize, $imageSize);
    $trans_color = imagecolorallocate($image, 255, 255, 255);
    imagefill($image, 0, 0, $trans_color);

    // then overwirte the source image to the new created image
    imagecopymerge($image, $src_image, 0, 0, 0, 0, $imageSize, $imageSize, 100);

    // then just copy all the rounded corner images to the 4 corners
    $radius = 20;
    // lt
    $lt_corner = getyuan();
    imagecopymerge($image, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
    // lb
    $lb_corner = imagerotate($lt_corner, 90, $trans_color);
    imagecopymerge($image, $lb_corner, 0, $imageSize - $radius, 0, 0,
    $radius, $radius, 100);
    // rb
    $rb_corner = imagerotate($lt_corner, 180, $trans_color);
    imagecopymerge($image, $rb_corner, $imageSize - $radius, $imageSize
    - $radius, 0, 0, $radius, $radius, 100);
    // rt
    $rt_corner = imagerotate($lt_corner, 270, $trans_color);
    imagecopymerge($image, $rt_corner, $imageSize - $radius, 0, 0, 0, $radius, $radius, 100);

    // set the transparency
    imagecolortransparent($image,$trans_color);


    imagepng($image, $file2);
    imagedestroy($image);

  • 相关阅读:
    C语言I博客作业09
    C语言I博客作业08
    第十四周助教总结
    C语言I博客作业07
    第十三周助教总结
    C语言I博客作业06
    第十二周助教总结
    学期总结
    C语言I博客作业09
    C语言I博客作业08
  • 原文地址:https://www.cnblogs.com/yangchong/p/5360314.html
Copyright © 2020-2023  润新知