Uploading transparent images adds a black background - forms

I am trying to make a function to handle avatar uploads.
My problem is, when I upload a transparent image, it turned everything which is transparent black after I resize it.
I have tried using the imagesavealpha() & imagealphablending() options, but the background is still turning black.
It might just be me being blind and not seeing the problem in the code, but I have no idea why it does this.
I can confirm that after the image has been moved to the image/avatars folder just after being uploaded, the background is still transparent.
this is my current code, I have been testing with .png images:
function upload_avatar(){
$base_path = SYSTEM_PATH;
if($_FILES['avatar_img']['tmp_name'] != '') {
$id = md5($this->user_id());
$filename = $_FILES['avatar_img']['name'];
$file_basename = substr($filename, 0, strripos($filename, '.')); // strip extention
$file_ext = substr($filename, strripos($filename, '.')); // strip name
$filesize = $_FILES['avatar_img']['size'];
$newfilename = $id . $file_ext;
if ($file_ext == ".jpg" || $file_ext == ".JPG" || $file_ext == ".jpeg" || $file_ext == ".png" || $file_ext == ".gif"){
if($filesize <= 153600){
move_uploaded_file($_FILES['avatar_img']['tmp_name'], $base_path."/images/avatars/" . $newfilename);
//resize image form
list($width, $height) = getimagesize($base_path."/images/avatars/" . $newfilename);
$scale_height = $height/$width;
$scale_width = $width/$height;
//Find height and width of the image
if($width > $height && $width > 150){
$width_new = 150;
$height_new = round($width_new*$scale_height);
}else if($height > $width && $height > 150){
$height_new = 150;
$width_new = round($height_new*$scale_width);
}else{
$height_new = $height;
$width_new = $width;
}
switch($file_ext) {
case ".jpg" :
case ".jpeg":
$source = imagecreatefromjpeg($base_path."/images/avatars/" . $newfilename);
break;
case ".png" :
$source = imagecreatefrompng($base_path."/images/avatars/" . $newfilename);
break;
default:
$source = imagecreatefromgif($base_path."/images/avatars/" . $newfilename);
break;
}
$destination = imagecreatetruecolor($width_new, $height_new);
imagesavealpha($destination, true);
imagealphablending($destination, true);
imagecopyresized($destination, $source, 0, 0, 0, 0, $width_new, $height_new, $width, $height);
switch($file_ext) {
case ".jpg":
case ".jpeg":
imagejpeg($destination, $base_path."/images/avatars/" . $newfilename, 85);
break;
case ".png":
imagepng($destination, $base_path."/images/avatars/" . $newfilename, 8);
break;
default:
imagegif($destination, $base_path."/images/avatars/" . $newfilename, 85);
break;
}
return $newfilename;
}else{
$this->upload_avatar = '<br />But the avatar was not updated. The avatar\'s size exceeded the 150kb limit. ';
return '';
}
}else{
$this->upload_avatar = '<br />But the avatar was not updated. The avatar must be one of the following formats: jpg, jpeg, png or gif. ';
return '';
}
}else{
return '';
}
}
Any help would be appreciated as I am going nuts looking at this now.
Thanks!

This here may be what you're looking for. The second comment contains an example of resizing pngs and gifs while preserving transparency.
p.s. I'd have added this as a comment but I don't have the rights to do that yet.

Related

Why imagecreatefromjpeg not working for .jpg extension?

I have code for thumbnails which works great for gif, png and jpeg files but not for jpg.
Please help me how to make it work for jpg.
I get no errors and nothing in log file.
function create_thumb($src,$dest,$desired_width = false, $desired_height = false){
if (!$desired_height&&!$desired_width) return false;
$fparts = pathinfo($src);
$ext = strtolower($fparts['extension']);
if (!in_array($ext,array('gif','jpg','png','jpeg'))) return false;
if ($ext == 'gif') $resource = imagecreatefromgif($src);
else if ($ext == 'png') $resource = imagecreatefrompng($src);
else if ($ext == 'jpg' || $ext == 'jpeg') $resource = imagecreatefromjpeg($src);
$width = imagesx($resource);
$height = imagesy($resource);
if(!$desired_height) $desired_height = floor($height*($desired_width/$width));
if(!$desired_width) $desired_width = floor($width*($desired_height/$height));
$virtual_image = imagecreatetruecolor($desired_width,$desired_height);
imagecopyresized($virtual_image,$resource,0,0,0,0,$desired_width,$desired_height,$width,$height);
$fparts = pathinfo($dest);
$ext = strtolower($fparts['extension']);
if (!in_array($ext,array('gif','jpg','png','jpeg')))
$ext = 'jpg';$dest = $fparts['dirname'].'/'.$fparts['filename'].'.'.$ext;
if ($ext == 'gif') imagegif($virtual_image,$dest);
else if ($ext == 'png') imagepng($virtual_image,$dest,1);
else if ($ext == 'jpg' || $ext == 'jpeg') imagejpeg($virtual_image,$dest,72);
return array( 'width' => $width, 'height' => $height, 'new_width' => $desired_width, 'new_height'=> $desired_height, 'dest' => $dest ); }
I find solution for me...
$dest = $fparts['dirname'].'/'.$fparts['filename'].'.gif';
imagewebp($virtual_image,$dest,80);
Every image is gif and very small size ;)
Hope it will help to somebody.

socks 5 proxy on Perl

I am new in Perl and trying to understand this cod in Link : http://codepaste.ru/1374/but I have some problem in understanding this part of code:
while($client || $target) {
my $rin = "";
vec($rin, fileno($client), 1) = 1 if $client;
vec($rin, fileno($target), 1) = 1 if $target;
my($rout, $eout);
select($rout = $rin, undef, $eout = $rin, 120);
if (!$rout && !$eout) { return; }
my $cbuffer = "";
my $tbuffer = "";
if ($client && (vec($eout, fileno($client), 1) || vec($rout, fileno($client), 1))) {
my $result = sysread($client, $tbuffer, 1024);
if (!defined($result) || !$result) { return; }
}
if ($target && (vec($eout, fileno($target), 1) || vec($rout, fileno($target), 1))) {
my $result = sysread($target, $cbuffer, 1024);
if (!defined($result) || !$result) { return; }
}
if ($fh && $tbuffer) { print $fh $tbuffer; }
while (my $len = length($tbuffer)) {
my $res = syswrite($target, $tbuffer, $len);
if ($res > 0) { $tbuffer = substr($tbuffer, $res); } else { return; }
}
while (my $len = length($cbuffer)) {
my $res = syswrite($client, $cbuffer, $len);
if ($res > 0) { $cbuffer = substr($cbuffer, $res); } else { return; }
}
}
can any body explain to me exactly whats happen in these lines:
vec($rin, fileno($client), 1) = 1 if $client;
vec($rin, fileno($target), 1) = 1 if $target;
and
select($rout = $rin, undef, $eout = $rin, 120);
Basically, the select operator is used to find which of your file descriptors are ready (readable, writable or there's an error condition). It will wait until one of the file descriptors is ready, or timeout.
select RBITS, WBITS, EBITS, TIMEOUT
RBITS is a bit mask, usually stored as a string, representing a set of file descriptors that select will wait for readability. Each bit of RBITS represent a file descriptor, and the offset of the file descriptor in this bit mask should the file descriptor number in system. Thus, you could use vec to generate this bit mask.
vec EXPR, OFFSET, BITS
The vec function provides storage of lists of unsigned integers. EXPR is a bit string, OFFSET is the offset of bit in EXPR, and BITS specifies the width of each element you're reading from / writing to EXPR.
So these 2 lines:
vec($rin, fileno($client), 1) = 1;
vec($rin, fileno($target), 1) = 1;
They made up a bit mask string $rin with setting the bit whose offset equals the file descriptor number of $client, as well as the one of $target.
Put it into select operator:
select($rout = $rin, undef, $eout = $rin, 120);
Then select will monitor the readability of the two file handlers ($client and $target), if one of them is ready, select will return. Or it will return after 120s if no one is ready.
WBITS, EBITS use the same methodology. So you could infer that the above select line will also return when the two file handler have any exceptions.

Why isn't my og:image picture updated properly on Facebook?

Hello all and thanks in advance for your help. Our website, Tom's Guide France has recently changed its logo.
Therefore, we updated the picture file that was linked in our og:image meta on pages not having a specific one.
Example URL : http://www.tomsguide.fr/solutions/id-2109117/desimlockage-dallas-urgent.html
The picture for the og:image meta is the following : http://m.bestofmedia.com/sfp/images/design/logos/fb/tgu_pic.jpg
For me, it's a text logo with an up/down arrow instead of the letter I.
But for Facebook, it's our old logo, with a torchlight.
I used the debugger for both those URL.
But in both cases, Facebook keeps seeing my old logo.
Any hint ?
Best,
Gzav
public static function resize_img($srcimg,$dest_img,$max_width=0,$max_height=0) {
$save = $dest_img;
$src = null;
$source_pic = $srcimg;
$imageinfo = getimagesize($source_pic);
//print_r($imageinfo);die;
switch($imageinfo['mime'])
{
case 'image/png':
$src = imagecreatefrompng($source_pic);
break;
case 'image/jpeg':
$src = imagecreatefromjpeg($source_pic);
break;
case 'image/gif':
$src = imagecreatefromgif($source_pic);
break;
case 'image/x-ms-bmp':
$src = imagecreatefrombmp($source_pic);
break;
}
list($width,$height) = getimagesize($source_pic);
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height) ){
$tn_width = $width;
$tn_height = $height;
}
elseif($y_ratio == 0){
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
elseif($x_ratio == 0){
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
elseif (($x_ratio * $height) < $max_height){
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
// $ext = strtolower(array_pop(explode(".", $srcimg)));
$ext=substr($srcimg,-3,3);
//ini_set('memory_limit', '32M');
$tmp=imagecreatetruecolor($tn_width,$tn_height);
//print_r($tmp.$src);die;
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
if(($ext=="jpg") || ($ext=="jpeg"))
{
$ye = imagejpeg($tmp, $save, 100) ;
}
if($ext=="gif"){ imagegif($tmp, $save) ; }
if($ext=="bmp"){ imagebmp($tmp, $save) ; }
if($ext=="png"){
//imagecolortransparent($tmp, imagecolorallocate($tmp,0,0,0));
imagepng($tmp, $save, 9) ;
}
}
$this->resize_img($siurcename,$dest_img2,83,83);//calling function

Zend for picture uploads needing to resize images

So, I am using Zend to handle my image uploads. This script works well, but I was wondering if there is a way to resize the image that is being uploaded no matter what the current size is. I've seen 2 similar posts, but their code was entirely different, thus would be difficult to translate into mine from theirs. If possible, I would really like to not have to use extensions, but I will if I have to. Any ideas?
if (isset($_POST['upload'])) {
require_once('scripter/lib.php');
//try {
$destination = 'C:\----';
$uploader = new Zend_File_Transfer_Adapter_Http();
$uploader->setDestination($destination);
$filename = $uploader->getFileName(NULL, FALSE);
$uploader->addValidator('Size', FALSE, '10000kB');
$uploader->addValidator('ImageSize', FALSE, array('minheight' => 100, 'minwidth' => 100));
//$pic = $filename;
if (!$uploader->isValid() || $errors) {
$messages = $uploader->getMessages();
} else {
//$pic = $filename;
$no_spaces = str_replace(' ', '_', $filename, $renamed);
$uploader->addValidator('Extension', FALSE, 'gif, png, jpg');
$recognized = FALSE;
if ($uploader->isValid()) {
$recognized = TRUE;
} else {
$mime = $uploader->getMimeType();
$acceptable = array('jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif');
$key = array_search($mime, $acceptable);
if (!$key) {
$messages[] = 'Unrecognized image type';
} else {
$no_spaces = "$no_spaces.$key";
$recognized = TRUE;
$renamed = TRUE;
}
}
$uploader->clearValidators();
if ($recognized) {
$existing = scandir($destination);
if (in_array($no_spaces, $existing)) {
$dot = strrpos($no_spaces, '.');
$base = substr($no_spaces, 0, $dot);
$extension = substr($no_spaces, $dot);
$i = 1;
do {
$no_spaces = $base . '_' . $i++ . $extension;
} while (in_array($no_spaces, $existing));
$renamed = TRUE;
}
$uploader->addFilter('Rename', array('target' => $no_spaces));
$success = $uploader->receive();
if (!$success) {
$messages = $uploader->getMessages();
} else {
//$pic = $no_spaces;
$uploaded = "$filename uploaded successfully";
$pic = $filename;
if ($renamed) {
$pic = "imgs/upld/" . $no_spaces;
$uploaded .= " and renamed $no_spaces";
//$pic = $no_spaces;
//$pic = $uploader->getFileName(NULL, FALSE);
} else {$pic = "imgs/upld/" . $filename;;}
$messages[] = "$uploaded";
//$pic = $no_spaces;
}
Zend Framework does not ship with a component for handling images.
Good News! PHP has several components that are really good at dealing with all kinds of image issues.
GD (one of those great PHP extensions) is currently shipped as a core extension for PHP, perhaps you will find it useful.
Maybe this will help: http://phpcodeforbeginner.blogspot.com/2013/04/resize-image-or-crop-image-using-gd.html
(not really trying to be too snarky ;)

Zend Framework - how to upload a file

How can i upload a file? Stackoverflow i cant page codes so please check this link: http://pastebin.org/386639
Thanks in advance
LATER:
Later i updated this as following, because ZF is not friendly, they keep still everything top secret! :P
public static function mvUploadFile()
{
//
// $_GET/_POST/FILE what ever
//
$fname = basename( $_FILES['attachment']['name']);
$_fname = strtolower (end(explode('.',$fname) ) );
Zend_Debug::dump( $_FILES );
//
// Filter the file
//
switch($_fname)
{
case ($_fname == 'jpg' ||
$_fname == 'jpeg' ||
$_fname == 'gif' ||
$_fname == 'bmp' ||
$_fname == 'png' ||
$_fname == 'html' ||
$_fname == 'pdf' ||
$_fname == 'doc' ||
$_fname == 'docx' ||
$_fname == 'xls'
):
$target_path = APPLICATION_PATH . "/../public/files/textual_translation_attachment/";
//chmod("../up" , 0777);
$target_path = $target_path . basename( $_FILES['attachment']['name']);
if(move_uploaded_file($_FILES['attachment']['tmp_name'], $target_path)) {
//echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
//$_sql = "insert into a (huis,image) values ('$_app','$_file')";
// send the file name only .....
//echo $fname ;
}else{
//echo "error ";
}
break;
}
return $fname;
}
If you
Zend_Debug::dump($_FILES);
Do you see your file?
If you are using jQuery, you may want to try Uploadify. This can be done using the
Zend Framework Uploadify Extension
http://gondo.webdesigners.sk/zend-framework-uploadify-extension/