2012-06-29 44 views
13

Trong ứng dụng của tôi, tôi có một hình ảnh trong một div, một nút.Cách xoay hình ảnh và lưu hình ảnh

Tôi muốn xoay hình ảnh được hiển thị và lưu hình ảnh được xoay khi tôi nhấp vào nút bằng cách sử dụng jquery.

tôi đã sử dụng mã:

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

và mã jquery: Mã

$(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" /> 

Khi tôi sử dụng trên mã, Có có hai hình ảnh một là hình ảnh cũ và một là hình ảnh xoay.

Ở đây tôi muốn xoay cùng một hình ảnh và chỉ hiển thị hình ảnh được xoay .Và lưu thư mục ina hình ảnh được xoay.

Tôi có thể làm điều này bằng cách sử dụng jquery như thế nào? Nếu nó không thể với jquery thì làm thế nào tôi có thể làm điều đó có thể với php/ajax?

+4

Bạn không thể lưu dữ liệu bằng JavaScript. Sử dụng AJAX để lưu hình ảnh. –

+0

Tôi có thể làm điều này bằng cách nào? –

+1

Xem bài đăng này http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html –

Trả lời

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"); 

Hãy xem tại địa chỉ:

imagerotate()

Và:

file_put_contents()

+7

Bạn thực sự cần sử dụng ['imagepng()'] (http://www.php.net/manual/en/function .imagepng.php) để ghi tập tin, không phải 'file_put_contents()'. – ggutenberg

+0

Cảm ơn. Nhưng tôi không thể lưu hình ảnh xoay bằng cách sử dụng file_put_contents(). Tôi đã sử dụng hàm imagejpeg() để thay thế. – Juljan

+0

Sử dụng imagepng() hoặc imagejpeg() thay vì file_put_contents(). – Phuong

10

ảnh xoay: PNG hoặc JPEG phụ thuộc vào loại tập tin với lưu trên máy chủ của bạn

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

Nó phù hợp với tôi, hãy thử.

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; 
       } 
     } 

    ?>