Image resizing not working? - php

I used to do this by percentages although now I am looking to do it by Pixels instead, although after trial, the images don't seem to be resizing? And I am not sure why?
$get_resize_width = '225';
$get_resize_height = '150';
$info = getimagesize($file);
list($width, $height) = getimagesize($file);
$new_width = $get_resize_width;
$new_height = $get_resize_height;
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($file);
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($file);
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($file);
$image = imagecreatetruecolor($new_width, $new_height);
$photo = imagecreatefromjpeg($file);
imagecopyresampled($image, $photo, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image, $newfile, $img_quality);
$origional_image = $file;
$compressed_image = $newfile;
I have tried adding px to the end of the variables although this doesn't seem to make a difference?

Related

GD library perform differently in the server

I have a problem in my images using the GD library in my live site.
Is there an issue of GD library in linux? I implemented the resizing and cropping of image using the GD library, but somehow only the resizing works. Also, png images have black background after resized. My codes are working totally fine in local but not in my hosted site. I didn't get any errors so I am not sure where the problem is.
This is my code:
$info = getimagesize($src);
$source_image = '';
if ($info['mime'] == 'image/jpeg')
$source_image = imagecreatefromjpeg($src);
elseif ($info['mime'] == 'image/gif')
$source_image = imagecreatefromgif($src);
elseif ($info['mime'] == 'image/png')
$source_image = imagecreatefrompng($src);
$cropped = imagecropauto($source_image, IMG_CROP_DEFAULT);
if ($cropped !== false) {
imagedestroy($source_image);
$source_image = $cropped;
}
$width = imagesx($source_image);
$height = imagesy($source_image);
$maxHeight = floor($height * ($maxWidth / $width));
$dst = imagecreatetruecolor($maxWidth, $maxHeight);
$background = imagecolorallocate($dst, 0, 0, 0);
imagecolortransparent($dst, $background);
imagealphablending($dst, false);
imagesavealpha($dst,true);
imagecopyresampled($dst, $source_image, 0, 0, 0, 0, $maxWidth, $maxHeight, $width, $height);
if ($info['mime'] == 'image/jpeg')
imagejpeg($dst, $newFilename);
elseif ($info['mime'] == 'image/gif')
imagegif($dst, $newFilename);
elseif ($info['mime'] == 'image/png')
imagepng($dst, $newFilename);
Please help. Thank You
Replace IMG_CROP_DEFAULT with IMG_CROP_SIDES
also read documentation click here you also fill color using
<?php
// Create a 300x300px transparant image with a 100px wide red circle in the middle
$i = imagecreatetruecolor(300, 300);
imagealphablending($i, FALSE);
imagesavealpha($i, TRUE);
$transparant = imagecolorallocatealpha($i, 0xDD, 0xDD, 0xDD, 0x7F);
imagecolortransparent($i, $transparant); // Set background transparent
imagefill($i, 0, 0, $transparant);
$red = imagecolorallocate($i, 0xFF, 0x0, 0x0);
imagefilledellipse($i, 150, 150, 100, 100, $red);
imagepng($i, "red_300.png");
// Crop away transparant parts and save
$i2 = imagecropauto($i, IMG_CROP_DEFAULT); //Attempts to use IMG_CROP_TRANSPARENT and if it fails it falls back to IMG_CROP_SIDES.
imagepng($i2, "red_crop_trans.png");
imagedestroy($i2);
// Crop away bg-color parts and save
$i2 = imagecropauto($i, IMG_CROP_SIDES);
imagepng($i2, "red_crop_sides.png");
imagedestroy($i2);
// clean up org image
imagedestroy($i);
?>

How to resize image inside dir using php?

This is my code, for resize test/15378596684270_1.jpg from 136x136px to 48x48px but when test code , its' not resize any image how can i do ?
<?PHP
session_start();
include("connect.php");
$file = "test/15378596684270_1.jpg";
list($width, $height) = getimagesize($file);
$newwidth = "48";
$newheight = "48";
$image_info = getimagesize($file);
if ($image_info['mime'] == 'image/jpeg') {
$src = imagecreatefromjpeg($file);
} elseif ($image_info['mime'] == 'image/gif') {
$src = imagecreatefromgif($file);
} elseif ($image_info['mime'] == 'image/png') {
$src = imagecreatefrompng($file);
}
$tgt = imagecreatetruecolor(48, 48);
imagecopyresampled($tgt, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
?>

Image compression seems to create black image for PNG

I have a basic image upload/compression script on my website and most images seem to work fine. Having an issue with PNG for some reason, whenever I try to add a PNG image, the actual image is uploaded but the compressed image is saved as a full black square?
I can't seem to find what could be wrong with this script? The images were saving without a filetype, so I did edit the code to upload all as JPG to try to fix the error, it did fix what they were being saved as but the black images remain the same?
Can anybody point me in the right direction here?
if(isset($_POST['upload_images'])){
if(count($_FILES['upload']['name']) > 0){
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$dirname = "galleries/".$portfolio_ref."/";
$filePath = $dirname.$_FILES['upload']['name'][$i];
$new_filename = date('dmYHis').rand(100000,9999999);
$new_filename2 = $dirname.$new_filename.'.jpg';
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $new_filename2)) {
$file = $new_filename2;
$data = $filePath;
$cut_name = substr($data, strpos($data, "/") + 1);
$cut_name = explode('/',$cut_name);
$cut_name = end($cut_name);
$newfile = $dirname.'thb_'.$new_filename.'.jpg';
$info = getimagesize($file);
list($width, $height) = getimagesize($file);
$max_width = 300;
$max_height = 400;
//try max width first...
$ratio = $max_width / $width;
$new_width = $max_width;
$new_height = $height * $ratio;
//if that didn't work
if ($new_height > $max_height) {
$ratio = $max_height / $height;
$new_height = $max_height;
$new_width = $width * $ratio;
}
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($file);
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($file);
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($file);
elseif ($info['mime'] == 'image/jpg') $image = imagecreatefromjpeg($file);
elseif ($info['mime'] == 'image/JPEG') $image = imagecreatefromjpeg($file);
elseif ($info['mime'] == 'image/PNG') $image = imagecreatefrompng($file);
elseif ($info['mime'] == 'image/GIF') $image = imagecreatefromgif($file);
elseif ($info['mime'] == 'image/JPG') $image = imagecreatefromjpeg($file);
$image = imagecreatetruecolor($new_width, $new_height);
$photo = imagecreatefromjpeg($file);
imagecopyresampled($image, $photo, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image, $newfile, 70);
$origional_image = $file;
$compressed_image = $newfile;
$digit_date = date("dmy");
$upload_date = date("Y-m-d");
$imgid = $digit_date.$i.rand();
mysqli_query($conn,"INSERT INTO umsikzw_images (image_id,portfolio_ref,thumb_link,full_link,image_name,date) VALUES ('$imgid','$portfolio_ref','$compressed_image','$origional_image','No Information','$upload_date')");
}
}
}
}
}

Php creating image thumb with fixed width and height

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]);

negative resized image

this is my script below which i use to re-size the images. My problem is that this script generates negative images (like negative films [with only .png files]) . Where/What is the problem ?
I used GD library to re-size the images but I got same result.
$dir = "../images/sliderimages/";
$photo = $_FILES['slid_image_upload']['name'];
$tmp_name = $_FILES['slid_image_upload']['tmp_name'];
$filename = $dir.$photo;
$dir_thm = "../images/thm_sliderimages/";
$thm_filename = $dir_thm.'thm_'.$photo;
/************Resizing the image***************/
$size = getimagesize($tmp_name);
$width = $size[0];
$height = $size[1];
$newheight = 200;
$newwidth = 420;
$newheight_thm = 50;
$newwidth_thm = 80;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$tmp_thm=imagecreatetruecolor($newwidth_thm, $newheight_thm);
if($size[2] == IMAGETYPE_GIF)
{
$src = imagecreatefromgif($tmp_name);
imagecopyresampled($tmp,$src, 0,0,0,0, $newwidth, $newheight, $width, $height);
imagecopyresampled($tmp_thm, $src, 0,0,0,0, $newwidth_thm, $newheight_thm, $width, $height);
imagegif($tmp,$filename,100);
imagegif($tmp_thm,$thm_filename,100);
}
elseif($size[2] == IMAGETYPE_JPEG)
{
$src = imagecreatefromjpeg($tmp_name);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp_thm, $src, 0,0,0,0, $newwidth_thm, $newheight_thm, $width, $height);
imagejpeg($tmp,$filename,100);
imagejpeg($tmp_thm,$thm_filename,100);
}
elseif($size[2] == IMAGETYPE_PNG)
{
$src = imagecreatefrompng($tmp_name);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp_thm, $src, 0,0,0,0, $newwidth_thm, $newheight_thm, $width, $height);
imagepng($tmp,$filename,9);
imagepng($tmp_thm,$thm_filename,9);
}
imagedestroy($src);
imagedestroy($tmp);
I'd advice you to try you code on another server\local machine to became sure that it's not current library installation issues.

Categories