This script helps me crop and save images. every time i save an image it saves has crop.jpg .. can i save it in a different name? i have an input field with image name.. "$_POST['imgname']" how can i use this has image name for the cropped image?
<?php
$canvasImg = $_POST['img'];
$data = base64_decode($canvasImg);
$File = "captured.jpg";
$Handle = fopen($File, 'w');
fwrite($Handle, $data);
fclose($Handle);
$src_image = imagecreatefrompng($File);
$width = imagesx($src_image);
$height = imagesy($src_image);
$dst_x = 0;
$dst_y = 0;
$src_x = $width*0.350; // Crop Start X
$src_y = $height*0.165; // Crop Srart Y
$dst_w = $width*0.294; // Thumb width
$dst_h = $height*0.470; // Thumb height
$src_w = $dst_w; // $src_x + $dst_w
$src_h = $dst_h; // $src_y + $dst_h
$image = imagecreatetruecolor($dst_w, $dst_h);
imagealphablending($image, false);
$col=imagecolorallocatealpha($image,255,255,255,127);
imagefilledrectangle($image,0,0,$dst_w, $dst_h,$col);
imagealphablending($image,true);
imagecopyresampled($image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagealphablending($image,true);
imagealphablending($image,false);
imagesavealpha($image,true);
imagepng($image, "images/crop.jpeg");
imagedestroy($image);
?>
If I understand your question correctly, you just need to make a new filename for the imagepng() to use.
This would seem a simple solution:
$canvasImg = $_POST['img'];
$data = base64_decode($canvasImg);
$File = "captured.jpg";
...
// create a new filesname for the cropped image in $save_to
$save_to = 'images/';
$save_to .= str_replace(array('.jpg','.png'), '', $data ) // remove original extension if it existed
$save_to .= '_crop.jpg';
// use this new name to save the cropped image
imagepng($image, $save_to);
However your code does not seem to make much sence. You accept an image name in $_POST['img'] then save it to a file called capured.jpg and then open it as a .png in $src_image = imagecreatefrompng($File);
I assume you must only be accepting .png files, and in that case why save it as a .jpg.
See what I mean, there appears to be a whole lot of confusion in your code.
Related
I have been trying to implement a rating display procedure to rating in form of stars as passed by GET in a php file.
Here's the code for rate.php :
$filename= "rating.png";
$rating = $_GET['rating'];
list($w, $h, $type, $attr) = getimagesize($filename);
$src_im = imagecreatefrompng($filename);
$dst_im = imagecreatefrompng("rating_back.png");
$src_x = '0'; // begin x
$src_y = '0'; // begin y
$src_w = $w * $rating / 5; // width
$src_h = $h; // height
$dst_x = '0'; // destination x
$dst_y = '0'; // destination y
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
header("Content-type: image/png");
imagepng($dst_im);
imagedestroy($dst_im);
$rating is a float value as per the GET request.
But when I try to implement rate.php?rating=4.3 using the following images:
"rating.png":
"rating_back.png":
I get the following result :
What could be the possible errors which are leading to an abnormal background in the final image?
Please give the possible solutions also.
Thanks in advance :)
It looks like those are 24 bit PNG images with transparency. You need to tell GD to save the transparency info too. Add:
imagesavealpha($dst_im, true);
before rendering your image
I am making thumbnails and for some reason, my output is the correct size, but always black. I saw another Stack Overflow post on a similar topic but in his case, he was passing parameters incorrectly.
I am capturing an image from a video camera and then using this code:
$data = base64_decode($data); // the data will be saved to the db
$image = imagecreatefromstring($data); // need to create an image to grab the width and height
$img_width = imagesx($image);
$img_height = imagesy($image);
// calculate thumbnail size
$new_height = 100;
$new_width = floor( $img_width * ( 100 / $img_height ) );
// create a new temporary image
$new_image = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled( $new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
$url = IMGDIR.$imgname;
$thumburl = IMGDIR."thumb/".$imgname;
// save image and thumb to disk
imagepng($image,$url);
imagepng($new_image,$thumburl);
The result I get is both files saved to the proper directories, both the proper size, but the thumbnail is all black. There must be something simple I am missing. Any ideas?
Remember that PNG files have alpha channels. So be sure to use imagealphablending and imagesavealpha. Here they are integrated into your code.
$data = base64_decode($data); // the data will be saved to the db
$image = imagecreatefromstring($data); // need to create an image to grab the width and height
$img_width = imagesx($image);
$img_height = imagesy($image);
// calculate thumbnail size
$new_height = 100;
$new_width = floor( $img_width * ( 100 / $img_height ) );
// create a new temporary image
$new_image = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
$url = IMGDIR . $imgname;
$thumburl = IMGDIR . "thumb/" . $imgname;
// Set the image alpha blending settings.
imagealphablending($image, false);
imagealphablending($new_image, false);
// Set the image save alpha settings.
imagesavealpha($image, true);
imagesavealpha($new_image, true);
// save image and thumb to disk
imagepng($image,$url);
imagepng($new_image,$thumburl);
Try saving your image's alpha channel with imagesavealpha and passing true for the 2nd argument
imagesavealpha($image, true);
imagepng($image,$url);
imagesavealpha($new_image, true);
imagepng($new_image,$thumburl);
I have created a simple PHP script to crop an image that has been previously uploaded to the server by the user and save it in another folder as some kind of a thumbnail.
$src_x = $_POST['left']; // Crop start x
$src_y = $_POST['top']; // Crop start y
$dst_w = $_POST['dim']; // Thumb width
$dst_h = $_POST['dim']; // Thumb height
$src_w = $_POST['dim']; // $src_x + $dst_w
$src_h = $_POST['dim']; // $src_y + $dst_h
$contact = $_POST['contact'];
$ratio = $_POST['ratio'];
$file_tmp = $_POST['file_tmp'];
$file_ext = strtolower(end(explode('.', $file_tmp)));
$img_info = getimagesize($file_tmp);
if ($file_ext == 'png') {
$src = imagecreatefrompng($file_tmp);
}
else if ($file_ext == 'jpeg' || $file_ext == 'jpg') {
$src = imagecreatefromjpeg($file_tmp);
}
else if ($file_ext == 'gif') {
$src = imagecreatefromgif($file_tmp);
}
$dst = imagecreatetruecolor(154, 154);
imagecopyresampled($dst, $src, 0, 0, $src_x * $ratio, $src_y * $ratio, 154, 154, $src_w * $ratio, $src_h * $ratio);
$img_name = $contact.'.png';
imagepng ($dst, '../images/invitados/'.$img_name);
The script works 100% fine with all jpeg / jpg / gif / png's EXCEPT for those images that have been imported by the user with iPhoto... Does anyone know what is going on??? I am going crazy 'cause I have no idea where the problem might be... The script doesn't even return a black image, so it doesn't even get to create the png...
Please help!
Many thanks
Check the image format that has been exported from iPhoto.Photos from iPhoto can be exported as JPEG | PNG | TIFF.
Maybe the images that you are trying to crop is in a TIFF format which is not included on condition on your script.
Basically, Im developing an image cropping system. And I'm trying to resize & convert the images into jpeg and then allow the crop functions to execute. However, it resizes & converts no problem. But when I add the cropping function, the problem is it returns with a cropped black image. The information for this will be posted with jQuery Ajax. And I know the ajax posting works properly because I debugged it. I can provide you with my client side script although I don't beleive that would be necessary since the problem seems to be in the PHP.
Update: I have revised my script. The problem still persists.
$old_file_path = $user_data['profile_pic'];
$new_width = $_POST['width_img'];
$new_height = $_POST['height_img'];
$dst_x = 0;
$dst_y = 0;
$src_x = $_POST['left'];
$src_y = $_POST['top'];
$dst_w = $_POST['width'];
$dst_h = $_POST['height'];
$src_w = $_POST['width'];
$src_h = $_POST['height'];
$file_path = 'core/images/profile/'. substr(md5(time()), 0, 15) . '.' . 'jpg';
$old_file_name = $user_data['profile_pic'];
$allowed = array('jpg','jpeg','gif','png');
$file_extension= explode('.', $old_file_name);
$file_extn= strtolower(end($file_extension));
$image_p = imagecreatetruecolor($new_width, $new_height);
if($file_extn == 'jpg' || $file_extn == 'jpeg'){
$image = imagecreatefromjpeg($old_file_name);
}if ($file_extn == 'png'){
$image = imagecreatefrompng($old_file_name);
}
list($width, $height) = getimagesize($old_file_name);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $file_path);
//Begin cropping!
$dst_image = imagecreatetruecolor($dst_w,$dst_h);
$src_image = imagecreatefromjpeg($old_file_name);
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagejpeg($dst_image, $file_path);
crop_profile_image($session_user_id, $file_path, $old_file_path);
function crop_profile_image($user_id, $file_path, $old_file_path){
if(file_exists($old_file_path) === true){unlink($old_file_path);}
mysql_query("UPDATE `users` SET `profile_pic` = '". mysql_real_escape_string($file_path) ."' WHERE `user_id` = " . (int)$user_id);
}
use timthumb instead its a single file and very easy to use, and it has so many options. More information can be found at : http://code.google.com/p/timthumb/
Thanks.
You can find imagecreatetruecolor and add this function imagecolorallocate after it.
E.g:
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); //find this function
imagecolorallocate($newImage, 0, 0, 0); //add this function
imagecolortransparent($newImage, $transpatent);
I want to upload an image from disk, resize it, and then upload it to Amazon S3.
However, I cant get the proper image output from imagejpeg().
heres my code:
$sourceUrl = $_FILES['path']['tmp_name'];
$thumbWidth = '100';
$thumbid = uniqid();
$img = imagecreatefromjpeg($sourceUrl);
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// output the image
imagejpeg($tmp_img);
// upload thumbnail to s3
$s3->putObjectFile($tmp_img, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);
Firebug gives me this error :
illegal character
[Break on this error] (�����JFIF���������>CREATOR: g...(using IJG JPEG v62), default quality\n
If I modify imagejpeg this way,
imagejpeg($tmp_img, 'abc.jpg');
then I get the same error. :(
Can i get some help here please ?
If you check the documentation of imagejpeg you can see it outputs the image, it means the way you call it it gets sent to the browser. You can get it to save to a file the second way you call it - by passing a filename in the second parameter.
Also, $tmp_img is an image resource, not a ready-to-use image file.
I don't know how your upload function works, but: if you need the file contents to upload, do it like this:
ob_start();
imagejpeg($tmp_image);
$image_contents = ob_get_clean();
$s3->putObjectFile($image_contents, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);
if you need a filename to upload:
$filename = tempnam(sys_get_temp_dir(), "foo");
imagejpeg($tmp_image, $filename);
$s3->putObjectFile($filename, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);
You have to define the header:
header('Content-type: image/jpeg');
1) $tmp_img is a resource not a file. You probably need to save the image to disc and use that for putObjectFile
2) You probably need to tell S3 that the file you're uploading is of type image/jpeg
Well guys thank you very much again, I screwed around a bit more and combining that with your responses I got this working as follows :)
$thumbid .= ".jpg";
$img = imagecreatefromgif($sourceUrl);
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$path = '/var/www/1.4/wwwroot/cdn/'.$thumbid;
// output the image
if(imagegif($tmp_img, $path)){
$thumblink = "";
// upload thumbnail to s3
if($s3->putObjectFile($path, "mybucket", $thumbid, S3::ACL_PUBLIC_READ)){
$thumblink = "http://dtzhqabcdscm.cloudfront.net/".$thumbid;
imagedestroy($tmp_img);
}
return $thumblink;
}