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]);
Related
I have this code, for changing the width of the image:
function resize_img($file,$width,$height=null){
$ext = pathinfo($file, PATHINFO_EXTENSION);
if($ext=='jpg' || $ext=='jpeg'){
$img = imagecreatefromjpeg($file);
} else if($ext=='png'){
$img = imagecreatefrompng($file);
}
$img_w = imagesx($img);
$img_h = imagesy($img);
$ratio = $width / $img_w;
$new_h = $img_h * $ratio;
$new_h = $height??$new_h;
$new_img = imagecreatetruecolor($width,$new_h);
imagecopyresampled($new_img, $img, 0, 0, 0, 0, $width, $new_h, $img_w, $img_h);
if($ext=='jpg' || $ext=='jpeg'){
imagejpeg($new_img, $file, 82);
} else if($ext=='png'){
imagepng($new_img, $file);
}
}
I would like to add the ability to change the width along with the height while keeping the aspect. Now resolution is changing, but isn't filled (it's streched) How can I do that? Is this possible?
I need to add watermark on uploaded image and also resize it to make thumbnails and icons from that watermarked image. Below is my function code which is adding watermark but I can't find out how to resize image to given height and width:
function watermark_image_new($target, $wtrmrk_file, $newcopy, $extension, $w = 0, $h = 0) {
$watermark = imagecreatefrompng($wtrmrk_file);
imagealphablending($watermark, false);
imagesavealpha($watermark, true);
//resize code
if ($w != 0) {
list($width, $height) = getimagesize($target);
if ($extension == 'jpeg' || $extension == 'jpg') {
$img = imagecreatefromjpeg($target);
} else if ($extension == 'png') {
$img = imagecreatefrompng($target);
}
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $img, 0, 0, 0, 0, $w, $h, $width, $height);
}
$img_w = imagesx($img);
$img_h = imagesy($img);
$wtrmrk_w = imagesx($watermark);
$wtrmrk_h = imagesy($watermark);
$dst_x = ($img_w / 2) - ($wtrmrk_w / 2); // For centering the watermark on any image
$dst_y = ($img_h / 2) - ($wtrmrk_h / 2); // For centering the watermark on any image
imagecopy($img, $watermark, $dst_x, $dst_y, 0, 0, $wtrmrk_w, $wtrmrk_h);
if ($extension == 'jpeg' || $extension == 'jpg') {
imagejpeg($img, $newcopy, 100);
} else if ($extension == 'png') {
imagepng($img, $newcopy);
}
imagedestroy($img);
imagedestroy($watermark);
}
A simple example to use Imagick for solve your case. To render Watermark and resize it and output the changed image
<?php
$image = new Imagick();
$image->readImage("/path/to/image.jpg");
$watermark = new Imagick();
$watermark->readImage("/path/to/watermark.png");
// how big are the images?
$iWidth = $image->getImageWidth();
$iHeight = $image->getImageHeight();
$wWidth = $watermark->getImageWidth();
$wHeight = $watermark->getImageHeight();
if ($iHeight < $wHeight || $iWidth < $wWidth) {
// resize the watermark
$watermark->scaleImage($iWidth, $iHeight);
// get new size
$wWidth = $watermark->getImageWidth();
$wHeight = $watermark->getImageHeight();
}
// calculate the position
$x = ($iWidth - $wWidth) / 2;
$y = ($iHeight - $wHeight) / 2;
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, $x, $y);
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
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);
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.
I have a bit of code that generates thumbnails from an uploaded image. The only problem is that it cuts portrait images off, rather than resizing the portrait image to fit the height of the thumbnail, landscape images are fine. I was wondering if anyone could help me out to change the code so it will place portrait images inside the thumbnail properly? (if that makes sense?)
Here's the code:
$setSize = 150;
$setHSize = 113;
$jpeg_quality = 75;
list($width, $height, $type, $attr) = getimagesize($origPath);
$newW = $setSize;
$newH = round( $height * ( $setSize / $width ) );
$img_r = imagecreatefromjpeg($origPath) or notfound();
$dst_r = ImageCreateTrueColor( $setSize, $setHSize );
$heightOffset = round( ($setHSize-$newH)/2 );
$white = imagecolorallocate($dst_r, 255, 255, 255);
imagefilledrectangle($dst_r, 0, 0, $setSize, $setHSize, $white);
imagecopyresampled($dst_r, $img_r, 0, $heightOffset, 0, 0, $newW, $newH, $width, $height);
header("Content-type: image/jpeg");
imagejpeg($dst_r, $thbPath, $jpeg_quality);
I just don't fully understand the way php creates images, so any help would be appreciated :)
The way you compute $newW and $newH is incorrect for what you want. You are giving preference to the width, so most landscape images will look okay, but portrait will be cut off. Try the following instead:
// assume landscape and see how things look
$newW = $setSize;
$newH = round( $height * ( $setSize / $width ) );
if ($newH > $setHSize) {
// portrait image
$newH = $setHSize;
$newW = round( $width * ( $setHSize / $height ) );
}
I hope this helps!
Hope this helps.
<?php
define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);
include_once(DOCROOT."/dbc.php");
//**********************| Resize based on height
$photo_height = 350;
//**********************| Get the file from the post
$file = $_FILES['file'];
$path_thumbs = (DOCROOT."/gallery/");
$path_big = (DOCROOT."/trash/");
//**********************| Check permission
if (!is_writeable($path_thumbs)){
die ("Error: The directory <b>($path_thumbs)</b> is NOT writable");
}
if (!is_writeable($path_big)){
die ("Error: The directory <b>($path_big)</b> is NOT writable");
}
//**********************| Make sure you have a file
if (isset($file)){
$file_type = $_FILES['file']['type'];
$file_name = $_FILES['file']['name'];
$file_size = $_FILES['file']['size'];
$file_tmp = $_FILES['file']['tmp_name'];
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
//**********************| Make new name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//**********************| See the kind of file we have
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}
elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}
elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//**********************| Get the height and width
list($width, $height) = getimagesize($file_tmp);
$imgratio=$height/$width;
if ($photo_height >= $height){
//*********** Dont resize if the image is smaller then $photo_height = 350;
$newwidth = $width;
$newheight = $height;
}
elseif ($imgratio>1){
$newheight = $photo_height;
$newwidth = $photo_height/$imgratio;
}
else{
$newwidth = $photo_height;
$newheight = $photo_height*$imgratio;
}
if (function_exists(imagecreatetruecolor)){ $resized_img = imagecreatetruecolor($newwidth,$newheight); }
else{ die("Error: Please make sure you have GD library ver 2+");
}
imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext",'100');
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
}
move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
$newimage = "$rand_name.$file_ext";
}
else {
echo "Sorry, there was a problem, Please try again.\n\n";
}
adaptiveResizeImage
or
adaptiveCropThumblanil
in Imagick can help you