How to rotate image and save the image

前端 未结 5 1986
予麋鹿
予麋鹿 2020-12-03 02:03

In my application i have an image in a div,a button.

I want to rotate the image displayed and save the rotated image when i clicked on the button using jquery.

5条回答
  •  孤街浪徒
    2020-12-03 02:31

    Image rotation: PNG or JPEG depend on file type with save on your server

    // File and rotation
    $rotateFilename = '/var/www/your_image.image_type'; // PATH
    $degrees = 90;
    $fileType = strtolower(substr('your_image.image_type', strrpos('your_image.image_type', '.') + 1));
    
    if($fileType == 'png'){
       header('Content-type: image/png');
       $source = imagecreatefrompng($rotateFilename);
       $bgColor = imagecolorallocatealpha($source, 255, 255, 255, 127);
       // Rotate
       $rotate = imagerotate($source, $degrees, $bgColor);
       imagesavealpha($rotate, true);
       imagepng($rotate,$rotateFilename);
    
    }
    
    if($fileType == 'jpg' || $fileType == 'jpeg'){
       header('Content-type: image/jpeg');
       $source = imagecreatefromjpeg($rotateFilename);
       // Rotate
       $rotate = imagerotate($source, $degrees, 0);
       imagejpeg($rotate,$rotateFilename);
    }
    
    // Free the memory
    imagedestroy($source);
    imagedestroy($rotate);
    

    It's works for me, try it.

提交回复
热议问题