The following code is supposed to output a resized image in a string. Unfortunately it only outputs MQ== so where is my mistake? Help is really appreciated :)
<?php
// The file
$filename = 'tick.png';
// Set a maximum height and width
$width = 200;
$height = 200;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$newImage = imagepng($image_p, null, 100); // got in has to be from 0 to 9
echo base64_encode(file_get_contents($newImage)); // this still does not work°
?>
Compression level can't be 100. Only from 0 to 9.
imagepng($image_p, null, from 0 to 9);
Base64 encode you can do with:
ob_start();
imagepng($image_p, null, 9); // got in has to be from 0 to 9
$stream = ob_get_clean();
echo base64_encode($stream);
This is how i do it:
$Temp_Dump = $_FILES["file"]["tmp_name"];
// Get new sizes
list($width, $height) = getimagesize($Temp_Dump);
$newwidth = 90;
$newheight = 90;
// Load
$Temp_thumb = imagecreatetruecolor($newwidth, $newheight);
//$source = imagecreatefromjpeg($Temp_Dump);
if($extension == "jpg" OR $extension=='jpeg'){
$source = ImageCreateFromJpeg($Temp_Dump);
}elseif ($extension == "gif"){
$source = ImageCreateFromGIF($Temp_Dump);
}elseif ($extension == 'png'){
$source = imageCreateFromPNG($Temp_Dump);
}
// Resize
imagecopyresized($Temp_thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
Related
I need to resize image to 150 x 150 px and then upload it to Amazon S3
Following is the code:
$image = $_FILES["userImage"]["name"];
$fileTempName = $_FILES['userImage']['tmp_name'];
$new_width = 150;
$new_height = 150;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromstring(file_get_contents($fileTempName));
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, imagesx($image), imagesy($image));
$newFielName = tempnam(sys_get_temp_dir(), "tempfilename");
imagepng($image_p, $newFielName, 9);
$s3 = new S3(awsAccessKey, awsSecretKey);
//move the file
if ($s3->putObjectFile($fileTempName, "urimages", $newFielName, S3::ACL_PUBLIC_READ)) {
$image_link = 'https://s3-us-west-2.amazonaws.com/urimages/' . $newFielName . '';
$this->Product->saveField('image', $image_link);
}
Following is the link which i receive upon uploading : https://s3-us-west-2.amazonaws.com/urimages/C:/Users/LS/AppData/Local/Temp/9102.tmp
Could be you please help me debug the code
i think issue in path. Please create folder on s3 and make a valid
path according to that folder
https://s3-us-west-2.amazonaws.com/urimages/C:/Users/LS/AppData/Local/Temp/9102.tmp
Example :- Resampling an image proportionally
<?php
// The file
$filename = 'test.jpg';
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
?>
I'm using a pre-written script found at 1st Web Magazine - Generate Thumbnail On The Fly.
I copied the script over to my server, but it's giving me the following error: Parse error: syntax error, unexpected '$ratio_orig' (T_VARIABLE) in /storage/emulated/legacy/www/jollyroger/jollyroger/img/gallery_temp/thumbnail.php on line 23
I've went through the code and I cannot spot any actual errors. Everything seems to be in order.
Could someone take a look at this code and tell me if I am overlooking something? From what I can gather it seems that something in the list() function is throwing off the variables.
This is the code actually in my file:
<?php
// thumb width
$square = 150;
$large = 200;
$small = 100;
////////////////////////////////////////////////////////////////////////////////// square
if( isset($_GET["img"]) && ( $_GET["type"] == "square" || $_GET["type"] == "" ) ){
// thumb size
$thumb_width = $square;
$thumb_height = $square;
// align
$align = $_GET["align"];
// image source
$imgSrc = $_GET["img"];
$imgExt = substr($imgSrc,-3);
// image extension
if($imgExt == "jpg"){ $myImage = imagecreatefromjpeg($imgSrc); }
if($imgExt == "gif"){ $myImage = imagecreatefromgif($imgSrc); }
if($imgExt == "png"){ $myImage = imagecreatefrompng($imgSrc); }
// getting the image dimensions
list($width_orig, $height_orig) = getimagesize($imgSrc);
// ratio
$ratio_orig = $width_orig/$height_orig;
// landscape or portrait?
if ($thumb_width/$thumb_height > $ratio_orig) {
$new_height = $thumb_width/$ratio_orig;
$new_width = $thumb_width;
} else {
$new_width = $thumb_height*$ratio_orig;
$new_height = $thumb_height;
}
// middle
$x_mid = $new_width/2;
$y_mid = $new_height/2;
// create new image
$process = imagecreatetruecolor(round($new_width), round($new_height));
imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// alignment
if($align == ""){
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumb_width/2)), ($y_mid-($thumb_height/2)), $thumb_width, $thumb_height, $thumb_width, $thumb_height);
}
if($align == "top"){
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumb_width/2)), 0, $thumb_width, $thumb_height, $thumb_width, $thumb_height);
}
if($align == "bottom"){
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumb_width/2)), ($new_height-$thumb_height), $thumb_width, $thumb_height, $thumb_width, $thumb_height);
}
if($align == "left"){
imagecopyresampled($thumb, $process, 0, 0, 0, ($y_mid-($thumb_height/2)), $thumb_width, $thumb_height, $thumb_width, $thumb_height);
}
if($align == "right"){
imagecopyresampled($thumb, $process, 0, 0, ($new_width-$thumb_width), ($y_mid-($thumb_height/2)), $thumb_width, $thumb_height, $thumb_width, $thumb_height);
}
imagedestroy($process);
imagedestroy($myImage);
if($imgExt == "jpg"){ imagejpeg($thumb, null, 100); }
if($imgExt == "gif"){ imagegif($thumb); }
if($imgExt == "png"){ imagepng($thumb, null, 9); }
}
// normal
if(isset($_GET["img"]) && ($_GET["type"] == "large" || $_GET["type"] == "small" ) ){
if( $_GET["type"] == "large" ){ $thumb_width = $large; }
if( $_GET["type"] == "small" ){ $thumb_width = $small; }
// image source
$imgSrc = $_GET["img"];
$imgExt = substr($imgSrc,-3);
// image extension
if($imgExt == "jpg"){ $myImage = imagecreatefromjpeg($imgSrc); }
if($imgExt == "gif"){ $myImage = imagecreatefromgif($imgSrc); }
if($imgExt == "png"){ $myImage = imagecreatefrompng($imgSrc); }
//getting the image dimensions
list($width_orig, $height_orig) = getimagesize($imgSrc);
// ratio
$ratio_orig = $width_orig/$height_orig;
$thumb_height = $thumb_width/$ratio_orig;
// new dimensions
$new_width = $thumb_width;
$new_height = $thumb_height;
// middle
$x_mid = $new_width/2;
$y_mid = $new_height/2;
// create new image
$process = imagecreatetruecolor(round($new_width), round($new_height));
imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumb_width/2)), ($y_mid-($thumb_height/2)), $thumb_width, $thumb_height, $thumb_width, $thumb_height);
if($imgExt == "jpg"){ imagejpeg($thumb, null, 100); }
if($imgExt == "gif"){ imagegif($thumb); }
if($imgExt == "png"){ imagepng($thumb, null, 9); }
}
?>
Even this line in my editor is highlighted red which indicates an error, but since the variable is triggering a php error this one has not shown up in any logs yet. Looking over this line I cannot spot anything wrong at all.
if(isset($_GET["img"]) && ($_GET["type"] == "large" || $_GET["type"] == "small" ) ){
I copied the code direct from the website so I'm at a loss as to why it is not working. As I said I've looked it over and everything seems to be in order, so I'm at a complete loss.
Also this script is in the same directory as my image files. My test url looks like so: http://localhost:8080/jollyroger/jollyroger/img/gallery_temp/thumbnail.php?img=1959523_1501438810115481_7515978806557307096_n.jpg
I think I found something.
you have the following line:
list($width_orig, $height_orig) = getimagesize($imgSrc);
which seems really weird to me. You are equalling a function to a function which might cause the error on the next line.
If that's not the problem then either you forgot a ; or you tried to access the variable before you set its value somewhere.
Guys i know that this question is asked before but its not the same because i have tried others answers and nothing worked as I want.
I want to create thumbnail image using php and the thumbnail width="265px" and height="125px" and here is my code :
$image_width = $imageSize[0];
$image_height = $imageSize[1];
$new_size = ($image_width + $image_height) / ($image_width * ($image_height / 45)); //this will set any image to 125*70 which is a good thumbnail
$new_width = $image_width * $new_size;
$new_height = $image_height * $new_size;
if($ext == "gif"){
$old_image = imagecreatefromgif($real_image_path);
}else if($ext =="png"){
$old_image = imagecreatefrompng($real_image_path);
}else{
$old_image = imagecreatefromjpeg($real_image_path);
}
$new_image = imagecreatetruecolor($new_width, $new_height); //creating new image with a new color for quality
imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
I am not very sure about my comments but i just wrote them just in case
I think it need someone that is good in maths
Thanks and happy new year
No math required..
if($ext == "gif"){
$old_image = imagecreatefromgif($real_image_path);
}else if($ext =="png"){
$old_image = imagecreatefrompng($real_image_path);
}else{
$old_image = imagecreatefromjpeg($real_image_path);
}
$data = getimagesize($real_image_path);
$height = 125;
$width = 265;
$thumb = imagecreatetruecolor($width, $height);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagecopyresampled($thumb, $old_image, 0, 0, 0, 0, $width, $height, $data[0], $data[1]);
this script some how showing half of the image not the whole image... i am trying to figure this out what is wrong here
any help please
// The file
$ftimage = $_POST['feature_image'];
// maximum height 150 X width 150
$width = 150;
$height = 150;
// Dimensions Set
list($width_orig, $height_orig) = getimagesize($ftimage);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($ftimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
$image = imagejpeg($image_p, null, 100);
var_dump($image);
Try to replace it
$ftimage = $_POST['feature_image'];
with it:
$ftimage = $_POST['feature_image']['tmp_name'];
Or if in your POST request stores a URL of remote image, try something like that:
$image = imagecreatefromstring(file_get_contents($ftimage));
Do any of you know of a good php class I can use to download an image from a remote source, re-size it to 120x120 and save it with a file name of my choosing?
So basically I would have an image at "http://www.site.com/image.jpg" save to my web server "/images/myChosenName.jpg" as a 120x120 pixels.
Thanks
You can try this:
<?php
$img = file_get_contents('http://www.site.com/image.jpg');
$im = imagecreatefromstring($img);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = '120';
$newheight = '120';
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'/images/myChosenName.jpg'); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
?>
More information about PHP image function : http://www.php.net/manual/en/ref.image.php
You can resize keeping the ratio of image
$im = imagecreatefromstring($img);
$width_orig = imagesx($im);
$height_orig = imagesy($im);
$width = '800';
$height = '800';
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
If you're looking to have the ability to do this for both jpg and png file formats, here's what helped me:
$imgUrl = 'http://www.example.com/image.jpg';
// or $imgUrl = 'http://www.example.com/image.png';
$fileInfo = pathinfo($imgUrl);
$img = file_get_contents($imgUrl);
$im = imagecreatefromstring($img);
$originalWidth = imagesx($im);
$originalHeight = imagesy($im);
$resizePercentage = 0.5;
$newWidth = $originalWidth * $resizePercentage;
$newHeight = $originalHeight * $resizePercentage;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
if ($fileInfo['extension'] == 'jpg') {
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg($tmp, '/img/myChosenName.jpg', -1);
}
else if ($fileInfo['extension'] == 'png') {
$background = imagecolorallocate($tmp , 0, 0, 0);
imagecolortransparent($tmp, $background);
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagepng($tmp, '/img/myChosenName.png');
}
else {
// This image is neither a jpg or png
}
imagedestroy($tmp);
imagedestroy($im);
The extra code on the png side of things ensures that the saved image contains any and all transparent sections.