Compress the image according to the specified width (the size of the image after compression will also be compressed), and the width is equal
$ NewWidth = 600;
$ File_path = 'd:/myWorkspace/get_img/deal_img/jiaShuiYin/1.jpg ';
$ NewName = 'd:/myWorkspace/get_img/deal_img/jiaShuiYin/2.jpg ';
ResizeImage ($ newWidth, $ file_path, $ newName );
/*
Function: compress an image according to the specified width (the size of the compressed image is also compressed)
*/
Function resizeImage ($ maxwidth, $ file_path, $ name)
{
$ Im = imagecreatefromjpeg ($ file_path );
$ Pic_width = imagesx ($ im );
$ Pic_height = imagesy ($ im );
If ($ maxwidth & $ pic_width> $ maxwidth)
{
$ Ratio = $ maxwidth/$ pic_width;
$ Newwidth = $ maxwidth;
$ Newheight = $ pic_height * $ ratio;
If (function_exists ("imagecopyresampled "))
{
$ Newim = imagecreatetruecolor ($ newwidth, $ newheight );
$ State = imagecopyresampled ($ newim, $ im, 0, 0, 0, $ newwidth, $ newheight, $ pic_width, $ pic_height );
} Else
{
$ Newim = imagecreate ($ newwidth, $ newheight );
$ State = imagecopyresized ($ newim, $ im, 0, 0, 0, $ newwidth, $ newheight, $ pic_width, $ pic_height );
}
If ($ state)
{
If (! Imagejpeg ($ newim, $ name )){
Write_log ($ name, "image write error ");
}
Imagedestroy ($ newim );
} Else
{
Write_log ($ name, "image write error ");
}
} Else
{
If (! Copy ($ file_path, $ name )){
Write_log ($ name, "image copy error ");
}
}
}