I am using this code for resizing:
<?php
// Function for resizing any jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
But everytime I upload a transparant png image it turns out with a black background. How do I prevent this from happening and maintain the transparant background?
try this before imagecopyresampled, added alpha blending, and then save alpa
imagealphablending($tci, false);
imagesavealpha($tci,true);
$transparent = imagecolorallocatealpha($tci, 255, 255, 255, 127);
imagefilledrectangle($tci, 0, 0, $w, $h, $transparent);
Try setting the transparent color using imagecolortransparent:
http://il.php.net/manual/en/function.imagecolortransparent.php
Try this, for making your re-size function
<?php
$file_name =testimg.jpg // your file name
extension= explode(".", strtolower($file_name));//get ext of your image
if($extension[1]=="jpg" || $extension[1]=="jpeg" )
{
$src = imagecreatefromjpeg($filename); // Check ext and set src
}
else if($extension[2]=="png")
{
$src = imagecreatefrompng($filename); // Check ext and set src
}
else
{
$src = imagecreatefromgif($filename); // Check ext and set src
}
list($width,$height)=getimagesize($filename);
$newwidth=round($width/10); //resized image width
$newheight=round($height/10); //resized image height
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$resizedfile = "resizeduploads/". $name;
if($extension[1]=="jpg" || $extension[1]=="jpeg" )
{
imagejpeg($tmp,$resizedfile);
}
else if($extension[1]=="png")
{
imagepng($tmp,$resizedfile);
}
else
{
imagegif($tmp,$resizedfile);
}
imagedestroy($src);
imagedestroy($tmp);
Related
I am using this code for resizing:
<?php
// Function for resizing any jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
But everytime I upload a transparant png image it turns out with a black background. How do I prevent this from happening and maintain the transparant background?
try this before imagecopyresampled, added alpha blending, and then save alpa
imagealphablending($tci, false);
imagesavealpha($tci,true);
$transparent = imagecolorallocatealpha($tci, 255, 255, 255, 127);
imagefilledrectangle($tci, 0, 0, $w, $h, $transparent);
Try setting the transparent color using imagecolortransparent:
http://il.php.net/manual/en/function.imagecolortransparent.php
Try this, for making your re-size function
<?php
$file_name =testimg.jpg // your file name
extension= explode(".", strtolower($file_name));//get ext of your image
if($extension[1]=="jpg" || $extension[1]=="jpeg" )
{
$src = imagecreatefromjpeg($filename); // Check ext and set src
}
else if($extension[2]=="png")
{
$src = imagecreatefrompng($filename); // Check ext and set src
}
else
{
$src = imagecreatefromgif($filename); // Check ext and set src
}
list($width,$height)=getimagesize($filename);
$newwidth=round($width/10); //resized image width
$newheight=round($height/10); //resized image height
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$resizedfile = "resizeduploads/". $name;
if($extension[1]=="jpg" || $extension[1]=="jpeg" )
{
imagejpeg($tmp,$resizedfile);
}
else if($extension[1]=="png")
{
imagepng($tmp,$resizedfile);
}
else
{
imagegif($tmp,$resizedfile);
}
imagedestroy($src);
imagedestroy($tmp);
Can you support me?
I have the following script in order to create a thumbnail, works fine!
BUT, when I upload a PNG file with transparent background, for some reason the background change to black.
<?php
// Function for resizing jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
If the background of the PNG it's transparent, I need the thumbnail transparent also, can you please support me?
Any help will be GREAT!
Thanks in advance
Try to replace this:
imagejpeg($tci, $newcopy, 80);
by this:
imagegif($tci, $newcopy, 80);
Or the equivalent function depending on the image format:
Imagepng
Imagegif
Imagejpeg
Imagebmp
Using:
$tci = imagecreate($w, $h);
Instead:
$tci = imagecreatetruecolor($w, $h);
My issue is now solved :)
I writing code that uploads an image from a url but I need to change the dimensions of the image to 250px * 250px when it's uploading.
How can I change my code to do that?
Example : If I upload an image of 130px * 185px dimensions, it's saving in my upload dir at 250px *250px automatically.
The code that I use is:
<?php
/* photo uploader*/
if($_POST["submit"]){
$pic = trim($_POST["pic_url"]);
if($pic){
$photo = fopen($pic,"rb");
if($photo){
$valid_exts = array("jpg","jpeg","gif","png","Bmp","TIFF"); // default image only extensions
if($valid_exts){
$newpic = fopen("../../cat/adventure/images/" . basename($pic), "wb"); // replace "downloads" with whatever directory you wish.
if($newpic){
while(!feof($photo)){
// Write the url file to the directory.
fwrite($newpic,fread($photo,1024 * 8),1024 * 8); // write the file to the new directory at a rate of 8kb/sec. until we reach the end.
}
}
}
}
}
}
?>
You can use the following universal function:
include_once("ak_php_img_lib_1.0.php");
$target_file = "uploads/$fileName";
$resized_file = "uploads/resized_$fileName";
$wmax = 200;
$hmax = 150;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
<?php
// Function for resizing jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
?>
I have a piece of code that seems to work just fine on processing jpegs on a live server, but processing png's or gif's always give me black images. The funny thing is that on my test server, it processes png's just fine, but no gifs either. This function should work well universally on all mime types, but I guess php is having problems finding the file paths of anything with a file extension other than jpg. Anybody have any ideas on modifiying my function to process png's and gif's correctly, besides switching to python?
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio; //if original image width is greater than height
} else {
$h = $w / $scale_ratio; //if original image height is greater than width
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);//gd functions
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);//makes a black rectangle with width and height you specify
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if ($ext == "gif"){
imagegif($tci, $newcopy);
} else if($ext =="png"){
imagepng($tci, $newcopy);
} else {
imagejpeg($tci, $newcopy, 84);
}
}
$file_name = $_FILES["uploaded_file"]["name"]; // The file name
$path_suffix = pathinfo($file_name);
$path_ext = $path_suffix['extension'];
$target_file = "uploads/$file_name";
$list_file = "uploads/list_$file_name";
$wmax = 400;
$hmax = 400;
ak_img_resize($target_file, $list_file, $wmax, $hmax, $path_ext);
I have personally tried your code, didn't touch the function, but modified the bottom part only and it works just fine.
This is how my working script looks like (I am getting the file name from the URL via GET, for debugging):
<?php
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio; //if original image width is greater than height
} else {
$h = $w / $scale_ratio; //if original image height is greater than width
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);//gd functions
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);//makes a black rectangle with width and height you specify
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if ($ext == "gif"){
imagegif($tci, $newcopy);
} else if($ext =="png"){
imagepng($tci, $newcopy);
} else {
imagejpeg($tci, $newcopy, 84);
}
}
$file_name = $_GET['img']; // The file name
$path_ext = substr($file_name, -3);
$target_file = $file_name;
$list_file = 'list_' . $file_name;
$wmax = 400;
$hmax = 400;
ak_img_resize($target_file, $list_file, $wmax, $hmax, $path_ext);
?>
Note that I tested on several different png, jpg and gif files and they were all resized just fine and should work for u too, unless there is some problem with the original image files themselves.
Hope this helps.
So I am using this code to resize an image on the fly and create a copy of it.
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
// imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
But what I don't like is it gives a default black background. How could I change this black background to something like, white?
Here it is:
After you initialize $tci, you can use imagefill() and imagecolorallocate() to fill the image with white:
$white = imagecolorallocate($tci, 255, 255, 255);
imagefill($tci, 0, 0, $white);
Note: this will only work if the source image uses transparent pixels.