2012-06-29 19 views
13

Uygulamamda, div düğmesindeki bir görüntü var.Görüntüyü döndürme ve görüntüyü kaydetme

Görüntülenen görüntüyü döndürmek ve jquery'yi kullanarak düğmeyi tıkladığımda döndürülen resmi kaydetmek istiyorum.

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

ve jquery kodu:

$(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 kodu:

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

i kodunun üzerinde kullanarak, var olan iki görüntü

Zaten kod kullanılmış biri eski görüntü ve diğeri döndürülen görüntüdür.

Burada aynı görüntüyü döndürmek ve yalnızca döndürülen resmi görüntülemek istiyorum. Döndürülmüş resmi bir dizine kaydedin.

Bunu jquery kullanarak nasıl yapabilirim? Eğer jquery ile mümkün olmazsa php/ajax ile nasıl yapılabilir?

+4

JavaScript'i kullanarak veri kaydedemezsiniz. Resmi kaydetmek için AJAX kullanın. –

+0

bunu nasıl yapabilirim? –

+1

Bu gönderiye bakın http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html –

cevap

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

bir göz atın:

imagerotate()

Ve:

file_put_contents()

+7

Aslında, ['imagepng()'] özelliğini kullanmanız gerekiyor (http://www.php.net/manual/en/function .imagepng.php) dosyaya yazmak, 'file_put_contents()'. – ggutenberg

+0

Teşekkürler. Ama ben file_put_contents() kullanarak döndürülen görüntü kaydedemiyorum. Bunun yerine imagejpeg() işlevini kullandım. – Juljan

+0

file_put_contents() yerine imagepng() veya imagejpeg() kullanın. – Phuong

10

Görüntü rotasyon: PNG veya JPEG sunucunuzda

tasarruf ile dosya türüne bağlıdır
// 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); 

Benim için çalışıyor, dene.

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

    ?>