2012-06-29 6 views
13

В моем приложении у меня есть изображение в div, кнопку.Как повернуть и сохранить изображение

Я хочу повернуть изображение и сохранить повернутое изображение, когда я нажал кнопку, используя jquery.

Я уже использовал код:

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

и JQuery код:

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

Когда я с помощью выше кода, Там есть два изображения один - это старое изображение, а другое - повернутое изображение.

Здесь я хочу повернуть одно и то же изображение и отобразить только повернутое изображение. И сохранить повернутое изображение в каталоге.

Как я могу это сделать с помощью jquery? Если это невозможно с jquery, то как я могу сделать это с помощью php/ajax?

+4

Вы не можете сохранять данные, используя JavaScript. Используйте AJAX для сохранения изображения. –

+0

как я могу это сделать? –

+1

Посмотреть это сообщение http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html –

ответ

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

Взгляните:

imagerotate()

И:

file_put_contents()

+7

Вам действительно нужно использовать ['imagepng()'] (http://www.php.net/manual/en/function .imagepng.php), чтобы записать файл, а не 'file_put_contents()'. – ggutenberg

+0

Спасибо. Но я не могу сохранить повернутое изображение, используя file_put_contents(). Вместо этого я использовал функцию imagejpeg(). – Juljan

+0

Используйте imagepng() или imagejpeg() вместо file_put_contents(). – Phuong

10

Изображение вращения: PNG или JPEG зависит от типа файла с сохранить на сервере

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

Это работает для меня, попробуйте.

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

    ?> 
Смежные вопросы