2012-06-29 12 views
13

W mojej aplikacji mam obraz w dziale, przycisk.Jak obrócić obraz i zapisać obraz

Chcę obrócić wyświetlany obraz i zapisać obrócony obraz po kliknięciu przycisku za pomocą jquery.

ja już używany kod:

http://code.google.com/p/jquery-rotate/

i kod jquery: kod

$(function() {         // doc ready 
       var rotation = 0;        // variable to do rotation with 
       $("#img").click(function() { 
        rotation = (rotation + 45) % 360; // the mod 360 probably isn't needed 
        $("#cropbox").rotate(rotation); 
       }); 
      }); 

html:

<img src="demo_files/pool.jpg" id="cropbox" /> 
<input type="button" id="img" name="img" value="click" /> 

Kiedy przy użyciu powyżej kodu Istnieją dwa obrazy jeden to stary obraz, a drugi to obrócony obraz.

Tutaj chcę obrócić ten sam obraz i wyświetlać tylko obrócony obraz. I zapisać obrócony obraz w katalogu.

Jak mogę to zrobić za pomocą jquery? Jeśli nie jest to możliwe z jquery, to jak mogę to zrobić za pomocą php/ajax?

+4

Nie można zapisać danych przy użyciu JavaScript. Użyj AJAX, aby zapisać obraz. –

+0

jak mogę to zrobić? –

+1

Zobacz ten post http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html –

Odpowiedz

15
//define image path 
$filename="image.jpg"; 

// Load the image 
$source = imagecreatefromjpeg($filename); 

// Rotate 
$rotate = imagerotate($source, $degrees, 0); 

//and save it on your server... 
imagejpeg($rotate, "myNEWimage.jpg"); 

Spójrz:

imagerotate()

I:

file_put_contents()

+7

Musisz użyć ['imagepng()'] (http://www.php.net/manual/en/function .imagepng.php), aby zapisać plik, a nie 'file_put_contents()'. – ggutenberg

+0

Dzięki. Ale nie mogę zapisać obróconego obrazu za pomocą file_put_contents(). Zamiast tego użyłem funkcji imagejpeg(). – Juljan

+0

Użyj imagepng() lub imagejpeg() zamiast file_put_contents(). – Phuong

10

Obrót obrazu: PNG lub JPEG zależy od typu pliku z zaoszczędzić na serwerze

// 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' || $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); 

To działa dla mnie, spróbuj.

2
<?php //image rotate code here 
     if(isset($_POST['save'])) 
     { 
      $degrees=90; 

      $new_file=$sourceName; 
      $filename ="http://localhost/dostoom/files_user/1000/4/153.jpg"; 
      $rotang = $degrees; 
      list($width, $height, $type, $attr) = getimagesize($filename); 
       $size = getimagesize($filename); 
       switch($size['mime']) 
       { 
       case 'image/jpeg': 
            $source = 
     imagecreatefromjpeg($filename); 
            $bgColor=imageColorAllocateAlpha($source, 0, 0, 
     0, 0); 
            $rotation = imagerotate($source, 
     $rotang,$bgColor); 
            imagealphablending($rotation, false); 
            imagesavealpha($rotation, true); 
            imagecreate($width,$height); 
            imagejpeg($rotation,$new_file); 
            chmod($filename, 0777); 
       break; 
       case 'image/png': 

            $source = 
     imagecreatefrompng($filename); 
            $bgColor=imageColorAllocateAlpha($source, 0, 0, 
     0, 0); 
            $rotation = imagerotate($source, 
     $rotang,$bgColor); 
            imagealphablending($rotation, false); 
            imagesavealpha($rotation, true); 
            imagecreate($width,$height); 
            imagepng($rotation,$new_file); 
            chmod($filename, 0777); 
       break; 
       case 'image/gif': 

            $source = 
     imagecreatefromgif($filename); 
            $bgColor=imageColorAllocateAlpha($source, 0, 0, 
     0, 0); 
            $rotation = imagerotate($source, 
     $rotang,$bgColor); 
            imagealphablending($rotation, false); 
            imagesavealpha($rotation, true); 
            imagecreate($width,$height); 
            imagegif($rotation,$new_file); 
            chmod($filename, 0777); 
       break; 
       case 'image/vnd.wap.wbmp': 
            $source = 
     imagecreatefromwbmp($filename); 
            $bgColor=imageColorAllocateAlpha($source, 0, 0, 
     0, 0); 
            $rotation = imagerotate($source, 
     $rotang,$bgColor); 
            imagealphablending($rotation, false); 
            imagesavealpha($rotation, true); 
            imagecreate($width,$height); 
            imagewbmp($rotation,$new_file); 
            chmod($filename, 0777); 
       break; 
       } 
     } 

    ?>