Cropping image in PHP

前端 未结 6 561
不思量自难忘°
不思量自难忘° 2020-11-30 11:01

I\'d like crop an image in PHP and save the file. I know your supposed to use the GD library but i\'m not sure how. Any ideas?

Thanks

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 12:05

    You can use below method to crop image,

    /*parameters are 
        $image =source image name
        $width = target width
        $height = height of image
        $scale = scale of image*/
        function resizeImage($image,$width,$height,$scale) {
            //generate new image height and width of source image
            $newImageWidth = ceil($width * $scale);
            $newImageHeight = ceil($height * $scale);
            //Create a new true color image
            $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
            //Create a new image from file 
            $source = imagecreatefromjpeg($image);
            //Copy and resize part of an image with resampling
            imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
            //Output image to file
            imagejpeg($newImage,$image,90);
            //set rights on image file
            chmod($image, 0777);
            //return crop image
            return $image;
        }
    

提交回复
热议问题