So I have an image uploaded what I'm trying to do is have the user rotate it, receive that value and rotate the image and replace the original with the new image.
So I have a button:
Rotate
In my js file:
$('.rotate').on('click', function(e){
e.preventDefault();
if(rotate < 360)
rotate = rotate +90;
else
rotate = 0;
var id = $(this).data('id');
$('.img-container img').attr('style', '-webkit-transform: rotate(' + rotate + 'deg)');
$.ajax({
type: "POST",
url: "functions/post",
data: { rotate: rotate, id: id }
});
});
and inside "functions/post":
$degrees = $_POST['rotate'];
$postid = $_POST['postid'];
//user the postid to get the imagefilepath from db
$filename = '../img/uploads/' . $post->getimagepath();
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
imagejpeg($rotate);
list($width, $height) = getimagesize($rotate);
$newImage = imagecreatetruecolor($width, $height);
imagecopyresampled($newImage, $source, 0, 0, 0, 0, $width, $height, imagesx($source), imagesy($source));
imagejpeg($newImage, realpath('../img/uploads/' . $post->getimagepath()));
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
imagedestroy($newImage);
but it doesn't save the rotated file.. :(
this code i got it from searching through so and google but can't seem to make it work for me yet. None of this code is set in stone so anything that needs changing aslong as it makes it work then its all good and greatly appreciated.
You're not saving the rotated image, you're saving the original image over again.
Try this:
$degrees = $_POST['rotate'];
$postid = $_POST['postid'];
//user the postid to get the imagefilepath from db
$filename = '../img/uploads/' . $post->getimagepath();
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
imagejpeg($rotate, realpath('../img/uploads/' . $post->getimagepath()));
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
(not tested) but you get the idea.
Related
My image file size is 800x600 and I want to resize this 800x600 to 400x300 then save both images(800x600 and 400x300) in the database base64_encode format. I can save in database first image (800x600) but how to convert a second image (400x300) in base64_encode format and save in database? I don't want to use two input fields. I think one input field enough for that.
$image = ($_FILES["my_image"]["name"]);
$theme_image = ($_FILES["my_image"]["tmp_name"]);
$bin_string = file_get_contents("$theme_image");
$theme_image_enc = base64_encode($bin_string);
You have to make a little script for create the new image from the first one and make the base64_encode on it
$WIDTH = 400; // The size of your new image
$HEIGHT = 300; // The size of your new image
$QUALITY = 100; //The quality of your new image
$DESTINATION_FOLDER = DependOfYourRepository; // The folder of your new image
// The directory where is your image
$filePath = DependOfYourRepository;
// This little part under depend if you wanna keep the ratio of the image or not
list($width_orig, $height_orig) = getimagesize($filePath);
$ratio_orig = $width_orig/$height_orig;
if ($WIDTH/$HEIGHT > $ratio_orig) {
$WIDTH = $HEIGHT*$ratio_orig;
} else {
$HEIGHT = $WIDTH/$ratio_orig;
}
// The function using are different for png, so it's better to check
if ($file_ext == "png") {
$image = imagecreatefrompng($filePath);
} else {
$image = imagecreatefromjpeg($filePath);
}
// I create the new image with the new dimension and maybe the new quality
$bg = imagecreatetruecolor($WIDTH, $HEIGHT);
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopyresampled($bg, $image, 0, 0, 0, 0, $WIDTH, $HEIGHT, $width_orig, $height_orig);
imagedestroy($image);
imagejpeg($bg, $DESTINATION_FOLDER.$filename, $QUALITY);
$bin_string_little = file_get_contents($DESTINATION_FOLDER.$filename);
// I remove the image created because you just wanna save the base64 version
unlike($DESTINATION_FOLDER.$filename);
imagedestroy($bg);
$theme_image_enc_little = base64_encode($bin_string_little);
// And now do what you want with the result
EDIT 1
It's possible to do it without using a directory for the second image but it's quite tricky.
$theme_image_little = imagecreatefromstring(base64_decode($theme_image_enc));
$image_little = imagecreatetruecolor($WIDTH, $HEIGHT);
// $org_w and org_h depends of your image, in your case, i guess 800 and 600
imagecopyresampled($image_little, $theme_image_little, 0, 0, 0, 0, $WIDTH, $HEIGHT, $org_w, $org_h);
// Thanks to Michael Robinson
// start buffering
ob_start();
imagepng($image_little);
$contents = ob_get_contents();
ob_end_clean();
$theme_image_enc_little = base64_encode($contents):
This is original image:
This is the code I am using:
<?php
$filename = 'image.jpg';
$degrees = 135;
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 1);
// Output
imagejpeg($rotate);
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>
This is output:
In this picture background is black. I want to make it white after rotation. Could you help me?
please you can change your code just like that
$source = imagecreatefromjpeg($filename);
// Rotate
$transColor = imagecolorallocatealpha($source, 255, 255, 255, 127);
$rotate = imagerotate($source, $degrees,$transColor);
and try this i hope it will help you in color you can change it with any another color code
I'm trying to build a function that takes a PHP image resource and places it in the center of a new image of predetermined size. I do not want to scale the image; rather I want to place it as-is in the center of an enlarged "canvas."
$img is a valid image resource - if I return it I receive the correct original (unprocessed) image. $canvas_w and $canvas_h are the width and height of the desired new canvas. It's creating the correct size canvas, but the contents of the file are unexpectedly solid black when I return the desired "corrected" image resource ($newimg).
// what file?
$file = 'smile.jpg';
// load the image
$img = imagecreatefromjpeg($file);
// resize canvas (not the source data)
$newimg = imageCorrect($img, false, 1024, 768);
// insert image
header("Content-Type: image/jpeg");
imagejpeg($newimg);
exit;
function imageCorrect($image, $background = false, $canvas_w, $canvas_h) {
if (!$background) {
$background = imagecolorallocate($image, 255, 255, 255);
}
$img_h = imagesy($image);
$img_w = imagesx($image);
// create new image (canvas) of proper aspect ratio
$img = imagecreatetruecolor($canvas_w, $canvas_h);
// fill the background
imagefill($img, 0, 0, $background);
// offset values (center the original image to new canvas)
$xoffset = ($canvas_w - $img_w) / 2;
$yoffset = ($canvas_h - $img_h) / 2;
// copy
imagecopy($img, $image, $xoffset, $yoffset, $canvas_w, $canvas_h, $img_w, $img_h);
// destroy old image cursor
//imagedestroy($image);
return $img; // returns a black original file area properly sized/filled
//return $image; // works to return the unprocessed file
}
Any hints or obvious errors here? Thanks for any suggestions.
In place of imagecopy(), this seemed to work:
imagecopymerge($img, $image, $xoffset, $yoffset, 0,0, $img_w, $img_h, 100);
Sure this is a easy one for you experts, I`m trying to upload a image to the site and save the path to the database. If possible, how can i change the image size before saving?
Here is my script that handles to update query when updating:
<?php // Get Event ID
$update=$_GET['update'];
// Create Session
$event_name=$_POST['event_name'];
$event_description=$_POST['event_description'];
$event_date=$_POST['event_date'];
$event_time=$_POST['event_time'];
$event_cost=$_POST['event_cost'];
$event_image=$_POST['event_image'];
// Connection to MySQL Database.
include ('_includes/_dbconnection.php');
include ('_includes/_dbopen.php');
// Update Event using Event ID.
$sql="UPDATE b_events SET ename = '$event_name', edescription = '$event_description', edate = '$event_date', etime = '$event_time', ecost = '$event_cost', eimage = '$event_image' WHERE id = '$update'";
$result=mysql_query($sql);
if($result){
header('Location: _events.php');
}
else {
header('Location: _home.php');
}
?>
I have the following code in the update.html for the image:
<input name="event_image" type="file" />
Thanks!!!
To change the image size must resize. A function to create a copy with specific dimensions.
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>
Source: http://php.net/manual/en/function.imagecopyresized.php
Php has GD library for working with images: function imagecopyresampled resize an image. Verify that GD is enalbed in you php config
http://us3.php.net/imagecopyresampled
I am working on improving my Facebook app. I need to be able to resize an image, then save it to a directory on the server. This is the code I have to resize:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
My question is, how would I save this resized image? Would I need to? Is there a way to manipulate the resized image without saving it?
According to the manual on imagejpeg(), the optional second parameter can specify a file name, which it will be written into.
Filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
To skip this argument in order to provide the quality parameter, use NULL.
It's usually a good idea to write the results to disk for some basic caching, so that not every incoming request leads to a (resource intensive) GD call.
function resize($img){
/*
only if you script on another folder get the file name
$r =explode("/",$img);
$name=end($r);
*/
//new folder
$vdir_upload = "where u want to move";
list($width_orig, $height_orig) = getimagesize($img);
//ne size
$dst_width = 110;
$dst_height = ($dst_width/$width_orig)*$height_orig;
$im = imagecreatetruecolor($dst_width,$dst_height);
$image = imagecreatefromjpeg($img);
imagecopyresampled($im, $image, 0, 0, 0, 0, $dst_width, $dst_height, $width_orig, $height_orig);
//modive the name as u need
imagejpeg($im,$vdir_upload . "small_" . $name);
//save memory
imagedestroy($im);
}
it should be work
http://www.php.net/manual/en/function.imagecopyresampled.php#90038