PHP - Unzip and convert images from .jpg to .bmp - php

I've a code that unzip files that I upload to my server, it works without any problem.
But now I want to add another function. I want to convert all images that are in .zip file (.zip file only contains images) from .jpg format to .bmp.
<?php
$file = $target_path1;
$path = $target_path2;
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
unlink($target_path1);
}
?>
There is some easy way to do it? If there is not, at least could I get name of extracted images?
Thanks.

You first create an image object out of your file with imagecreatefromjpeg(). You then dump that object into different formats (using imagegif() for example):
$imageObject = imagecreatefromjpeg($imageFile);
imagegif($imageObject, $imageFile . '.gif');
imagepng($imageObject, $imageFile . '.png');
imagewbmp($imageObject, $imageFile . '.bmp');

class ToBmp{
// new image width
var $new_width;
// new image height
var $new_height;
// image resources
var $image_resource;
function image_info($source_image){
$img_info = getimagesize($source_image);
switch ($img_info['mime']){
case "image/jpeg": { $this->image_resource = imagecreatefromjpeg ($source_image); break; }
case "image/gif": { $this->image_resource = imagecreatefromgif ($source_image); break; }
case "image/png": { $this->image_resource = imagecreatefrompng ($source_image); break; }
default: {die("unsupported image time");}
}
}
public function imagebmp($file_path = ''){
if(!$this->image_resource) die("cant not convert. image resource is null");
$picture_width = imagesx($this->image_resource);
$picture_height = imagesy($this->image_resource);
if(!imageistruecolor($this->image_resource)){
$tmp_img_reource = imagecreatetruecolor($picture_width,$picture_height);
imagecopy($tmp_img_reource,$this->image_resource, 0, 0, 0, 0, $picture_width, $picture_height);
imagedestroy($this->image_resource);
$this->image_resource = $tmp_img_reource;
}
if((int) $this->new_width >0 && (int) $this->new_height > 0){
$image_resized = imagecreatetruecolor($this->new_width, $this->new_height);
imagecopyresampled($image_resized,$this->image_resource,0,0,0,0,$this->new_width,$this->new_height,$picture_width,$picture_height);
imagedestroy($this->image_resource);
$this->image_resource = $image_resized;
}
$result = '';
$biBPLine = ((int) $this->new_width >0 &&(int)$this->new_height > 0) ? $this->new_width * 3 : $picture_width * 3;
$biStride = ($biBPLine + 3) & ~3;
$biSizeImage = ((int) $this->new_width >0 &&(int)$this->new_height > 0) ? $biStride * $this->new_height : $biStride * $picture_height;
$bfOffBits = 54;
$bfSize = $bfOffBits + $biSizeImage;
$result .= substr('BM', 0, 2);
$result .= pack ('VvvV', $bfSize, 0, 0, $bfOffBits);
$result .= ((int) $this->new_width >0 &&(int)$this->new_height > 0) ? pack ('VVVvvVVVVVV', 40, $this->new_width, $this->new_height, 1, 24, 0, $biSizeImage, 0, 0, 0, 0) : pack ('VVVvvVVVVVV', 40, $picture_width, $picture_height, 1, 24, 0, $biSizeImage, 0, 0, 0, 0);
$numpad = $biStride - $biBPLine;
$h = ((int) $this->new_width >0 &&(int)$this->new_height > 0) ? $this->new_height : $picture_height;
$w = ((int) $this->new_width >0 &&(int)$this->new_height > 0) ? $this->new_width : $picture_width;
for ($y = $h - 1; $y >= 0; --$y) {
for ($x = 0; $x < $w; ++$x) {
$col = imagecolorat ($this->image_resource, $x, $y);
$result .= substr(pack ('V', $col), 0, 3);
}
for ($i = 0; $i < $numpad; ++$i) {
$result .= pack ('C', 0);
}
}
if($file_path == ''){
header("Content-type: image/bmp");
echo $result;
} else {
$fp = fopen($file_path,"wb");
fwrite($fp,$result);
fclose($fp);
//=============
}
return ;
}
}
$ToBMP = new ToBmp();
$ToBMP->image_info('imgqr/'.$name.'image.png');
$ToBMP->new_width = 300;
$ToBMP->new_height = 300;
$ToBMP->imagebmp('imgqr/'.$name."image.bmp");

Related

Uploading profile pictures once (not twice) into database, and displaying them correctly

I am trying to built an option unto my site where users can upload a profile picture, so far so good, but the data is saved again and again in my database(table) when I refresh the page
++ the image doesn't show, it just shows a black square.
Thanks for any help, as you can see, I'm still an absolute beginner learning eagerly and ferociously
$images = (isset ($_FILES['images']['name']));
$paths= [];
if ($images)
{
$saveto = "uploads/$user.jpg";
move_uploaded_file($_FILES['images']['tmp_name'], $saveto);
$typeok = TRUE;
$paths[] = $saveto;
$date1=date("Y-m-d h:m:s");
$link=mysqli_connect("localhost","root","","vlindr");
mysqli_select_db($link,"imageupload"); //database connectivity
mysqli_query($link,"insert into images
(images_id, images_path, submission_date)
VALUES (NULL,'$user','$date1')")
or die(mysqli_error($link));
switch($_FILES['images']['type'])
{
case "image/gif": $src = imagecreatefromgif($saveto); break;
case "image/jpeg": // Both regular and progressive jpegs
case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
case "image/png": $src = imagecreatefrompng($saveto); break;
default: $typeok = FALSE; break;
}
if ($typeok)
{
list($w, $h) = getimagesize($saveto);
$max = 150;
$tw = $w;
$th = $h;
if ($w > $h && $max < $w) {
$th = $max ;
$tw = $max;
}
elseif ($h > $w && $max < $h) {
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w) {
$tw = $th = $max;
}
$tmp = imagecreatetruecolor($tw, $th);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imageconvolution($tmp, array(array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1)),
8, 0);
imagejpeg($tmp, $saveto);
imagedestroy($tmp);
imagedestroy($src);
}
}
Please let me know if the code below works.
I also recommend prepared statements, although the code below is not the neatest it will get the job done!
$images = (isset ($_FILES['images']['name']));
$paths= [];
if ($images)
{
$saveto = "uploads/$user.jpg";
move_uploaded_file($_FILES['images']['tmp_name'], $saveto);
$typeok = TRUE;
$paths[] = $saveto;
$date1=date("Y-m-d h:m:s");
$link=mysqli_connect("localhost","root","","vlindr");
mysqli_select_db($link,"imageupload"); //database connectivity
//Checks if Image already exists in database
$stmt = $link->prepare("SELECT * FROM images WHERE images_path=(?)");
$stmt->bind_param("s",$saveto);
$stmt->execute();
$stmt->store_result();
//If the amount of rows returned is zero, the script will insert the image into the database
if($stmt->num_rows === 0) {
//Inserts the image into the database
$stmt2 = $link->prepare("INSERT INTO images (images_id,images_path,submission_date) VALUES (NULL,?,?)");
$stmt2->bind_param("ss",$saveto,$date1);
if($stmt2->execute()) {
echo "Image is unique and uploaded to database";
}
switch($_FILES['images']['type'])
{
case "image/gif": $src = imagecreatefromgif($saveto); break;
case "image/jpeg": // Both regular and progressive jpegs
case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
case "image/png": $src = imagecreatefrompng($saveto); break;
default: $typeok = FALSE; break;
}
if ($typeok)
{
list($w, $h) = getimagesize($saveto);
$max = 150;
$tw = $w;
$th = $h;
if ($w > $h && $max < $w)
{
$th = $max ;
$tw = $max;
}
elseif ($h > $w && $max < $h)
{
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w)
{
$tw = $th = $max;
}
$tmp = imagecreatetruecolor($tw, $th);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imageconvolution($tmp, array(array(-1, -1, -1),
array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
imagejpeg($tmp, $saveto);
imagedestroy($tmp);
imagedestroy($src);
}
}//end if
else {
echo "Image Already Exists";
}//end else

php gd image png watermark on jpg file produces weird result

Trying to add a transparent PNG watermark to jpg images get's me different results either if i'm on localhost or production server.
Here's the results...
On my localhost (PHP Version 5.6.24)
On production server (PHP Version 5.5.9-1ubuntu4.21)
I don't even know what to call this problem of the blue color turning pink and the logo doesn't get correctly rendered...
The code is this:
function getBrightness($gdHandle) {
$width = imagesx($gdHandle);
$height = imagesy($gdHandle);
$totalBrightness = 0;
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
$rgb = imagecolorat($gdHandle, $x, $y);
$red = ($rgb >> 16) & 0xFF;
$green = ($rgb >> 8) & 0xFF;
$blue = $rgb & 0xFF;
$totalBrightness += (max($red, $green, $blue) + min($red, $green, $blue)) / 2;
}
}
// imagedestroy($gdHandle);
return ($totalBrightness / ($width * $height)) / 2.55;
}
function render_text_on_gd_image(&$source_gd_image, $text, $font, $size, $color, $opacity, $rotation, $bpx, $bpy, $wnewx) {
$source_width = imagesx($source_gd_image);
$source_height = imagesy($source_gd_image);
$bb = imagettfbbox($size, $rotation, $font, $text);
$x0 = min($bb[0], $bb[2], $bb[4], $bb[6]);
$x1 = max($bb[0], $bb[2], $bb[4], $bb[6]);
$y0 = min($bb[1], $bb[3], $bb[5], $bb[7]);
$y1 = max($bb[1], $bb[3], $bb[5], $bb[7]);
$bb_width = abs($x1 - $x0);
$bb_height = abs($y1 - $y0);
$alpha_color = imagecolorallocatealpha(
$source_gd_image,
hexdec(substr($color, 0, 2)),
hexdec(substr($color, 2, 2)),
hexdec(substr($color, 4, 2)),
127 * (100 - $opacity) / 100
);
return imagettftext($source_gd_image, $size, $rotation, $bpx + $wnewx/2 - $bb_width/2 , $bpy, $alpha_color, $font, $text);
}
$val = array();
//$val["displayName"] = "Administrador";
$val["displayName"] = "Afonso Ferreira Gomes";
$val["relFile"] = "imagem.jpg";
list($val["width_original"], $val["height_original"]) = getimagesize($val["relFile"]);
if ($val["width_original"] > $val["height_original"] && $val["width_original"] >= 1200) {
$val["height_resized"] = $val["height_original"] * (1200/$val["width_original"]);
$val["width_resized"] = 1200;
} elseif ($val["height_original"] > $val["width_original"] && $val["height_original"] >= 1200) {
$val["height_resized"] = 1200;
$val["width_resized"] = (1200/$val["height_original"]) * $val["width_original"];
} else {
$val["height_resized"] = $val["height_original"];
$val["width_resized"] = $val["width_original"];
}
$i = imagecreatefromjpeg($val["relFile"]);
$i = imagescale($i, $val["width_resized"], $val["height_resized"]);
$val["width_new"] = imagesx($i);
$val["height_new"] = imagesy($i);
if ($val["width_new"] < $val["height_new"]) {
$val["portait"] = true;
$val["smallest"] = $val["width_new"];
} else {
$val["portait"] = false;
$val["smallest"] = $val["height_new"];
}
/* GERAR WATERMARK LOGOTIPO */
$val["racio"] = 0.20;
$val["marLeft"] = 40;
$val["marBottom"] = 40;
$val["bright"] = getBrightness($i);
if ($val["bright"] < 30) {
$val["opacidade"] = 20;
} else {
$val["opacidade"] = 40;
}
$w = imagecreatefrompng("jbw_260_40.png");
if ($val["portait"]) {
$val["water_width"] = $val["width_new"] * $val["racio"];
$val["water_height"] = $val["width_new"] * $val["racio"];
} else {
$val["water_width"] = $val["height_new"] * $val["racio"];
$val["water_height"] = $val["height_new"] * $val["racio"];
}
$wnew = imagescale($w, $val["water_width"], $val["water_height"]);
$val["water_width"] = imagesx($wnew);
$val["water_height"] = imagesy($wnew);
/* GERAR WATERMARK TEXTO AUTOR */
if ($val["displayName"] == "Administrador") {
$val["autor"] = "";
$val["marBottom"] = 20;
$val["marLeft"] = 20;
} else {
$val["autor"] = $val["displayName"];
}
// PRIMEIRO E ULTIMO NOME SÓ!!
$autorNomes = explode(" ", $val["autor"]);
if (count($autorNomes) >= 2) {
$val["autor"] = $autorNomes[0] . " " . $autorNomes[count($autorNomes) - 1];
} else {
$val["autor"] = $autorNomes[0];
}
$font = "../font/Montserrat.ttf";
$color = "FFFFFF";
$flagSize = false;
$val["fontSize"] = 12;
for ($size = 7; $size <= 50; $size += 0.5) {
$bb = imagettfbbox($size, 0, $font, $val["autor"]);
$x0 = min($bb[0], $bb[2], $bb[4], $bb[6]);
$x1 = max($bb[0], $bb[2], $bb[4], $bb[6]);
$y0 = min($bb[1], $bb[3], $bb[5], $bb[7]);
$y1 = max($bb[1], $bb[3], $bb[5], $bb[7]);
$val["bbox_width"] = abs($x1 - $x0);
$val["bbox_height"] = abs($y1 - $y0);
if (($val["bbox_width"] >= $val["water_width"] && !$flagSize) || $size == 50) {
$val["fontSize"] = $size;
$flagSize = true;
break;
}
}
$val["posY_water"] = $val["height_new"] - $val["water_height"] - $val["marBottom"];
$val["posY_text"] = $val["height_new"] - $val["marBottom"] + $val["bbox_height"] - 5;
imagecopy($i, $wnew, $val["marLeft"], $val["posY_water"] , 0, 0, $val["water_width"], $val["water_height"]);
render_text_on_gd_image($i, $val["autor"], $font, $val["fontSize"], $color, $val["opacidade"] ,0 , $val["marLeft"] , $val["posY_text"], $val["water_width"]);
// echo "<pre>"; print_r($val); echo "</pre>"; die;
/* OUTPUT IMAGEM E LIMPA BUFFER */
header('Content-type: image/png');
imagepng($i);
imagedestroy($i);
imagedestroy($w);
And this are the gd section of phpinfo for localhost and production server
What am I missing? this is driving me insane!
Those issues almost always object to the alpha channel of either the source or destination not beeing preserved or the output not really beeing true color.
Try using the imagesavealpha() function and search for "alpha" in the comments of the PHP documentation because there are many people having solutions to similiar issues.
This comment might be helpful and is kinda what Esko said:
http://php.net/manual/en/function.imagecreatefrompng.php#90364
Basically it copies an image to a buffer using imagecreatetruecolor().
I'm having the same issue. The imageScale function in Ubuntu seems to turn greens and blues to pink. Solution could be to use imagecopyresampled and imagecreatetruecolor. However this solution is way slower.

Thumbnails with Face Detection in PHP

My objective with this script is to make smart thumbnails. In my demo package, I am using two scripts from different sources.
To crop thumbnails (Source) - It totally works like native wordpress thumbnails
Face Detection in PHP (Source)
I am using the Face Detection to get the desired coordinates (where the face is) and then feed the coordinates to the crop script to make a thumbnail.
The problem is, if the Face Detection script does not find a face, it'd just time-out with the time out error
Fatal error: Maximum execution time of 30 seconds exceeded in...
I do not know how to come around this issue.
Is there any way to limit the amount of time the face detector takes to detect? I mean, if not found in like 15 seconds, return null.
Here's the Face Detection code:
<?php
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// #Author Karthik Tharavaad
// karthik_tharavaad#yahoo.com
// #Contributor Maurice Svay
// maurice#svay.Com
namespace svay;
use Exception;
class FaceDetector
{
protected $detection_data;
protected $canvas;
protected $face;
private $reduced_canvas;
/**
* Creates a face-detector with the given configuration
*
* Configuration can be either passed as an array or as
* a filepath to a serialized array file-dump
*
* #param string|array $detection_data
*/
public function __construct($detection_data = 'detection.dat')
{
if (is_array($detection_data)) {
$this->detection_data = $detection_data;
return;
}
if (!is_file($detection_data)) {
// fallback to same file in this class's directory
$detection_data = dirname(__FILE__) . DIRECTORY_SEPARATOR . $detection_data;
if (!is_file($detection_data)) {
throw new \Exception("Couldn't load detection data");
}
}
$this->detection_data = unserialize(file_get_contents($detection_data));
}
public function faceDetect($file)
{
if (is_resource($file)) {
$this->canvas = $file;
} elseif (is_file($file)) {
//getting extension type (jpg, png, etc)
$type = explode(".", $file);
$ext = strtolower($type[sizeof($type)-1]);
$ext = (!in_array($ext, array("jpeg","png","gif"))) ? "jpeg" : $ext;
if ($ext == 'jpeg') {
$this->canvas = imagecreatefromjpeg($file);
} else if ($ext == 'png') {
$this->canvas = imagecreatefrompng($file);
} else if ($ext == 'gif') {
$this->canvas = imagecreatefromgif($file);
}
} else {
throw new Exception("Can not load $file");
}
$im_width = imagesx($this->canvas);
$im_height = imagesy($this->canvas);
//Resample before detection?
$diff_width = 320 - $im_width;
$diff_height = 240 - $im_height;
if ($diff_width > $diff_height) {
$ratio = $im_width / 320;
} else {
$ratio = $im_height / 240;
}
if ($ratio != 0) {
$this->reduced_canvas = imagecreatetruecolor($im_width / $ratio, $im_height / $ratio);
imagecopyresampled(
$this->reduced_canvas,
$this->canvas,
0,
0,
0,
0,
$im_width / $ratio,
$im_height / $ratio,
$im_width,
$im_height
);
$stats = $this->getImgStats($this->reduced_canvas);
$this->face = $this->doDetectGreedyBigToSmall(
$stats['ii'],
$stats['ii2'],
$stats['width'],
$stats['height']
);
if ($this->face['w'] > 0) {
$this->face['x'] *= $ratio;
$this->face['y'] *= $ratio;
$this->face['w'] *= $ratio;
}
} else {
$stats = $this->getImgStats($this->canvas);
$this->face = $this->doDetectGreedyBigToSmall(
$stats['ii'],
$stats['ii2'],
$stats['width'],
$stats['height']
);
}
return ($this->face['w'] > 0);
}
public function toJpeg()
{
$color = imagecolorallocate($this->canvas, 255, 0, 0); //red
imagerectangle(
$this->canvas,
$this->face['x'],
$this->face['y'],
$this->face['x']+$this->face['w'],
$this->face['y']+ $this->face['w'],
$color
);
header('Content-type: image/jpeg');
imagejpeg($this->canvas);
}
public function toJson()
{
return json_encode($this->face);
}
public function getFace()
{
return $this->face;
}
protected function getImgStats($canvas)
{
$image_width = imagesx($canvas);
$image_height = imagesy($canvas);
$iis = $this->computeII($canvas, $image_width, $image_height);
return array(
'width' => $image_width,
'height' => $image_height,
'ii' => $iis['ii'],
'ii2' => $iis['ii2']
);
}
protected function computeII($canvas, $image_width, $image_height)
{
$ii_w = $image_width+1;
$ii_h = $image_height+1;
$ii = array();
$ii2 = array();
for ($i=0; $i<$ii_w; $i++) {
$ii[$i] = 0;
$ii2[$i] = 0;
}
for ($i=1; $i<$ii_h-1; $i++) {
$ii[$i*$ii_w] = 0;
$ii2[$i*$ii_w] = 0;
$rowsum = 0;
$rowsum2 = 0;
for ($j=1; $j<$ii_w-1; $j++) {
$rgb = ImageColorAt($canvas, $j, $i);
$red = ($rgb >> 16) & 0xFF;
$green = ($rgb >> 8) & 0xFF;
$blue = $rgb & 0xFF;
$grey = (0.2989*$red + 0.587*$green + 0.114*$blue)>>0; // this is what matlab uses
$rowsum += $grey;
$rowsum2 += $grey*$grey;
$ii_above = ($i-1)*$ii_w + $j;
$ii_this = $i*$ii_w + $j;
$ii[$ii_this] = $ii[$ii_above] + $rowsum;
$ii2[$ii_this] = $ii2[$ii_above] + $rowsum2;
}
}
return array('ii'=>$ii, 'ii2' => $ii2);
}
protected function doDetectGreedyBigToSmall($ii, $ii2, $width, $height)
{
$s_w = $width/20.0;
$s_h = $height/20.0;
$start_scale = $s_h < $s_w ? $s_h : $s_w;
$scale_update = 1 / 1.2;
for ($scale = $start_scale; $scale > 1; $scale *= $scale_update) {
$w = (20*$scale) >> 0;
$endx = $width - $w - 1;
$endy = $height - $w - 1;
$step = max($scale, 2) >> 0;
$inv_area = 1 / ($w*$w);
for ($y = 0; $y < $endy; $y += $step) {
for ($x = 0; $x < $endx; $x += $step) {
$passed = $this->detectOnSubImage($x, $y, $scale, $ii, $ii2, $w, $width+1, $inv_area);
if ($passed) {
return array('x'=>$x, 'y'=>$y, 'w'=>$w);
}
} // end x
} // end y
} // end scale
return null;
}
protected function detectOnSubImage($x, $y, $scale, $ii, $ii2, $w, $iiw, $inv_area)
{
$mean = ($ii[($y+$w)*$iiw + $x + $w] + $ii[$y*$iiw+$x] - $ii[($y+$w)*$iiw+$x] - $ii[$y*$iiw+$x+$w])*$inv_area;
$vnorm = ($ii2[($y+$w)*$iiw + $x + $w]
+ $ii2[$y*$iiw+$x]
- $ii2[($y+$w)*$iiw+$x]
- $ii2[$y*$iiw+$x+$w])*$inv_area - ($mean*$mean);
$vnorm = $vnorm > 1 ? sqrt($vnorm) : 1;
$count_data = count($this->detection_data);
for ($i_stage = 0; $i_stage < $count_data; $i_stage++) {
$stage = $this->detection_data[$i_stage];
$trees = $stage[0];
$stage_thresh = $stage[1];
$stage_sum = 0;
$count_trees = count($trees);
for ($i_tree = 0; $i_tree < $count_trees; $i_tree++) {
$tree = $trees[$i_tree];
$current_node = $tree[0];
$tree_sum = 0;
while ($current_node != null) {
$vals = $current_node[0];
$node_thresh = $vals[0];
$leftval = $vals[1];
$rightval = $vals[2];
$leftidx = $vals[3];
$rightidx = $vals[4];
$rects = $current_node[1];
$rect_sum = 0;
$count_rects = count($rects);
for ($i_rect = 0; $i_rect < $count_rects; $i_rect++) {
$s = $scale;
$rect = $rects[$i_rect];
$rx = ($rect[0]*$s+$x)>>0;
$ry = ($rect[1]*$s+$y)>>0;
$rw = ($rect[2]*$s)>>0;
$rh = ($rect[3]*$s)>>0;
$wt = $rect[4];
$r_sum = ($ii[($ry+$rh)*$iiw + $rx + $rw]
+ $ii[$ry*$iiw+$rx]
- $ii[($ry+$rh)*$iiw+$rx]
- $ii[$ry*$iiw+$rx+$rw])*$wt;
$rect_sum += $r_sum;
}
$rect_sum *= $inv_area;
$current_node = null;
if ($rect_sum >= $node_thresh*$vnorm) {
if ($rightidx == -1) {
$tree_sum = $rightval;
} else {
$current_node = $tree[$rightidx];
}
} else {
if ($leftidx == -1) {
$tree_sum = $leftval;
} else {
$current_node = $tree[$leftidx];
}
}
}
$stage_sum += $tree_sum;
}
if ($stage_sum < $stage_thresh) {
return false;
}
}
return true;
}
}
Here's an example use:
include "facedetection/FaceDetector.php";
$detector = new svay\FaceDetector('detection.dat');
$detector->faceDetect($path);
$coord = $detector->getFace();
Any help or suggest other php thumbnails with face detection script.
Can you increase the time limit? You can use set_time_limit() or change your php.ini if you have access to it.
Also, how big is the detection.dat file? Loading the entire file in memory with file_get_contents() can take some time if the file is large.
Unless you mess up with the FaceDetection code (which is not recommended, unless you will never want to upgrade the library), you won't be able to stop the execution after 15 seconds. They don't provide any hooks where you could tell the script to stop.

imagecreatefrombmp BMP to JPG

Im using the the online code (https://github.com/hm2k/hm2k-php/blob/master/functions/imagecreatefrombmp.php) for converting "BMP" to "JPG"; I put in a separate file (in the includes library) as following:
class bmp2jpg {
/**
* #convert BMP to GD
* #param string $src
* #param string|bool $dest
* #return bool
*/
public static function bmp2gd($src, $dest = false) {
// open source file for reading
if (!($srch = fopen($src, 'rb'))) {
user_error('Unable to open source for reading.');
return false;
}
// open the destination file for writing
if (!($desth = fopen($dest, 'wb'))) {
user_error('Unable to open destination for writing.');
return false;
}
// get the headers
$header = unpack('vtype/Vsize/v2reserved/Voffset', fread($srch, 14));
// get the rest of the image
$info = unpack('Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant', fread($srch, 40));
// extract the header and info into varibles
extract($info);
extract($header);
// check for BMP signature
if ($type != 0x4D42) {
user_error('Source image is not a BMP.');
return false;
}
// set the pallete
$palette_size = $offset - 54;
$ncolor = $palette_size / 4;
// true-color vs. palette
$gd_header = '';
$gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
$gd_header .= pack('n2', $width, $height);
$gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
if ($palette_size) {
$gd_header .= pack('n', $ncolor);
}
// do not allow transparency
$gd_header .= "\xFF\xFF\xFF\xFF";
// write the destination headers
fwrite($desth, $gd_header);
unset($gd_header);
// if we have a palette
if ($palette_size) {
// read the palette
$palette = fread($srch, $palette_size);
// begin the gd palette
$gd_palette = '';
$j = 0;
// loop of the palette
while ( $j < $palette_size ) {
$b = $palette{$j++};
$g = $palette{$j++};
$r = $palette{$j++};
$a = $palette{$j++};
// assemble the gd palette
$gd_palette .= $r . $g . $b . $a;
}
// finish the palette
$gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);
// write the gd palette
fwrite($desth, $gd_palette);
unset($gd_palette);
}
// scan line size and alignment
$scan_line_size = (($bits * $width) + 7) >> 3;
$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0;
// main loop
for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
// create scan lines starting from bottom
fseek($srch, $offset + (($scan_line_size + $scan_line_align) * $l));
$scan_line = fread($srch, $scan_line_size);
$gd_scan_line = '';
if ($bits == 24) {
$j = 0;
while ( $j < $scan_line_size ) {
$b = $scan_line{$j++};
$g = $scan_line{$j++};
$r = $scan_line{$j++};
$gd_scan_line .= "\x00" . $r . $g . $b;
}
}
else if ($bits == 8) {
$gd_scan_line = $scan_line;
}
else if ($bits == 4) {
$j = 0;
while ( $j < $scan_line_size ) {
$byte = ord($scan_line{$j++});
$p1 = chr($byte >> 4);
$p2 = chr($byte & 0x0F);
$gd_scan_line .= $p1 . $p2;
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
else if ($bits == 1) {
$j = 0;
while ( $j < $scan_line_size ) {
$byte = ord($scan_line{$j++});
$p1 = chr((int)(($byte & 0x80) != 0));
$p2 = chr((int)(($byte & 0x40) != 0));
$p3 = chr((int)(($byte & 0x20) != 0));
$p4 = chr((int)(($byte & 0x10) != 0));
$p5 = chr((int)(($byte & 0x08) != 0));
$p6 = chr((int)(($byte & 0x04) != 0));
$p7 = chr((int)(($byte & 0x02) != 0));
$p8 = chr((int)(($byte & 0x01) != 0));
$gd_scan_line .= $p1 . $p2 . $p3 . $p4 . $p5 . $p6 . $p7 . $p8;
}
// put the gd scan lines together
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
// write the gd scan lines
fwrite($desth, $gd_scan_line);
unset($gd_scan_line);
}
// close the source file
fclose($srch);
// close the destination file
fclose($desth);
// return destination file
return $dest;
}
/**
* #ceate a BMP image
* #param string $filename
* #return bin string on success
* #return bool false on failure
*/
public static function imagecreatefrombmp($filename) {
// create a temp file
$tmpfile = tempnam(sys_get_temp_dir(), 'gd');
// convert to gd
if (bmp2gd($filename, $tmpfile)) {
// create image resource
$img = imagecreatefromgd($tmpfile);
}
// remove temp file
#unlink($tmpfile);
return isset($img) ? $img : false;
}
}
and Im calling the function this way:
require_once ("..\includes\imagecreatefrombmp.php");
$GD_Employee = bmp2jpg::bmp2gd("..\img\Rep.bmp");
$dest=bmp2jpg::imagecreatefrombmp($GD_Employee);
but it gives me the following errors:
Warning: require_once(..\includes\imagecreatefrombmp.php): failed to open stream
Fatal error: require_once(): Failed opening required '..\includes\imagecreatefrombmp.php'
Could you please let me know why? and am I supposed to pass the image name like "..\img\Rep.bmp"
Thanks
Because it can't find imagecreatefrombmp.php ... check if it is in right directory, it counts from the first run php file.

Preserving GIF tansparencies when rebuilding thumbnails

On upload of images, the code creates several different thumbnails. In some cases, the thumbnail may be larger than the original image, in which case a padding is applied. In the case of JPEGs, the padding is white, and all is well. In the case of GIFs and PNGs, the padding should be transparent. And it is. Or not. Which is weird.
If I was getting an entirely black or transparent padding, I'd have some idea where the problem is, but I'm getting paddings which are transparent in places and black in others, which makes no sense.
Sample PNG and GIF images at http://filedump.xn--es-zka.info/broken_thumbs/ (on a shocking pink background colour so you can see the difference between white and transparent).
Any bright ideas?
class Image {
public static function exec($path) {
// This function receives the path to an image.
// Logic here skipped: For each thumbnail, check whether Crop is set. If it is, call self::crop(). Otherwise, call self::resize().
return true;
}
public static function resize($file, $class, $width='-', $height='-', $fit=0) {
$a = getimagesize($file);
switch ($a[2]) {
case 1:
$tag = 'gif';
break;
case 2:
$tag = 'jpeg';
break;
case 3:
$tag = 'png';
break;
default:
return;
}
$w = $a[0];
$h = $a[1];
if ($width == 0) {
$fw = 0;
$fh = $h / $height;
} elseif ($height == 0) {
$fw = $w / $width;
$fh = 0;
} else {
$fw = $w / $width;
$fh = $h / $height;
}
if (($fw == 1) and ($fh == 1)) {
$file2 = dirname($file) . '/' . $class . basename($file);
copy($file, $file2);
return true;
} elseif (($fw >= 1) and ($fh >= 1)) {
if ($fw > $fh) {
$w = $width;
$h = floor($h / $fw);
} else {
$h = $height;
$w = floor($w / $fh);
}
} elseif ($fh == 0) {
if ($fw > 1) {
$fit = 0;
$w = $width;
$h = floor($h / $fw);
} else {
if ($fit) {
$height = $h;
}
}
} elseif ($fw == 0) {
if ($fh > 1) {
$fit = 0;
$w = floor($w / $fh);
$h = $height;
} else {
if ($fit) {
$width = $w;
}
}
} elseif (($fw < 1) and ($fh < 1)) {
//
} elseif ($fw > $fh) {
if ($fw >= 1) {
$w = $width;
$h = floor($h / $fw);
}
} elseif ($fh > $fw) {
if ($fh >= 1) {
$w = floor($w / $fh);
$h = $height;
}
}
if ($fit) {
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
$cw = $width;
$ch = $height;
} else {
$x = 0;
$y = 0;
$cw = $w;
$ch = $h;
}
$file2 = dirname($file) . '/' . $class . basename($file);
$f1 = 'imagecreatefrom' . $tag;
$src = $f1($file);
$new = imagecreatetruecolor($cw, $ch);
return self::create($new, $src, $file2, $x, $y, $w, $h, $a);
}
public static function crop($file, $class, $width='-', $height='-', $fit=0) {
if (!$class) return trigger_error('ExecImage: Original image can not be overwritten.');
$small = 0;
$a = getimagesize($file);
switch ($a[2]) {
case 1:
$tag = 'gif';
break;
case 2:
$tag = 'jpeg';
break;
case 3:
$tag = 'png';
break;
default:
return;
}
$w = $a[0];
$h = $a[1];
if ($height == 0) {
//resize by width -- height will follow
$fh = 0;
$fw = $w / $width;
} elseif ($width == 0) {
//resize by height -- width will follow
$fw = 0;
$fh = $h / $height;
} else {
$fw = $w / $width;
$fh = $h / $height;
}
if (($fw <= 1) and ($fh <= 1)) {
$small = 1;
//don't resize
} else {
$fit = 1;
//chop by the smallest
if ($fh < $fw) {
//Crop By Height
$h = $height;
$w = floor($w / $fh);
//$w = $width;
//$h = floor($h /$fw);
//$w = $width;
} else {
//Crop By Width
$w = $width;
$h = floor($h /$fw);
//$h = $height;
//$w = floor($w / $fh);
}
}
$file2 = dirname($file) . '/' . $class . basename($file);
$f1 = 'imagecreatefrom' . $tag;
$src = $f1($file);
if ($small) {
if ($fit) {
//image must be padded
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
} else {
//image goes as is -- shrinked
$x = 0;
$y = 0;
}
} else {
//image must be centered -- this should be a square from js
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
}
if ($small) {
if ($fit) {
//create with the full size
$new = imagecreatetruecolor($width, $height);
} else {
//nah, just with the original size
$new = imagecreatetruecolor($w, $h);
}
} else {
if ($fit) {
$new = imagecreatetruecolor($width, $height);
} else {
$new = imagecreatetruecolor($w, $h);
}
}
return self::create($new, $src, $file2, $x, $y, $w, $h, $a);
}
private static function create($new, $src, $file2, $x, $y, $w, $h, $a) {
switch ($a[2]) {
case 1: // GIF
case 3: // PNG
// http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/
// http://www.mummey.org/2008/11/transparent-gifs-with-php-and-gd/
imagealphablending($new, false);
imagesavealpha($new, true);
$back = imagecolorallocatealpha($new, 255, 255, 255, 127);
imagefilledrectangle($new, 0, 0, $w, $h, $back);
imagecolortransparent($new, $back);
break;
case 2: // JPEG
$back = imagecolorallocate($new, 255, 255, 255);
break;
}
imagefill($new, 0, 0, $back);
imagecopyresampled($new, $src, $x, $y, 0, 0, $w, $h, $a[0], $a[1]);
if (file_exists($file2)) unlink($file2);
switch ($a[2]) {
case 1:
imagegif($new, $file2);
break;
case 2:
imagejpeg($new, $file2, 100);
break;
case 3:
imagepng($new, $file2, 0);
break;
}
imagedestroy($src);
imagedestroy($new);
return true;
}
}
Revision/Edit to add:
Well, I've solved the main problem: padding added to PNG or GIF images is now transparent, instead of black. The smaller problem, that GIF transparency becomes black, is still with us, but I don't use GIF transparency much.
<?php
class Image {
public static function exec($path) {
$file = Nearest::file('.config.php', dirname($path) . '/', BD . '/images/');
if (!file_exists($file)) exit('The ".config.php" file doesnt exist');
require $file;
foreach ($config['operations'] as $k => $v) {
if (!isset($v['class'])) $v['class'] = '.' . $k . '.';
if (Core::val($v, 'crop')) {
self::crop($path, $v['class'], Core::val($v, 'width', '-'), Core::val($v, 'height', '-'), Core::val($v, 'fit', 0));
} else {
self::resize($path, $v['class'], Core::val($v, 'width', '-'), Core::val($v, 'height', '-'), Core::val($v, 'fit', 0));
}
}
return true;
}
public static function delete($path) {
$a = glob(dirname($path) . '/.*.' . basename($path));
foreach ($a as $v) unlink($v);
unlink($path);
}
public static function resize($file, $class, $width='-', $height='-', $fit=0) {
$a = getimagesize($file);
switch ($a[2]) {
case 1:
$tag = 'gif';
break;
case 2:
$tag = 'jpeg';
break;
case 3:
$tag = 'png';
break;
default:
return;
}
$w = $a[0];
$h = $a[1];
if ($width == 0) {
$fw = 0;
$fh = $h / $height;
} elseif ($height == 0) {
$fw = $w / $width;
$fh = 0;
} else {
$fw = $w / $width;
$fh = $h / $height;
}
if (($fw == 1) and ($fh == 1)) {
$file2 = dirname($file) . '/' . $class . basename($file);
copy($file, $file2);
return true;
} elseif (($fw >= 1) and ($fh >= 1)) {
if ($fw > $fh) {
$w = $width;
$h = floor($h / $fw);
} else {
$h = $height;
$w = floor($w / $fh);
}
} elseif ($fh == 0) {
if ($fw > 1) {
$fit = 0;
$w = $width;
$h = floor($h / $fw);
} else {
if ($fit) {
$height = $h;
}
}
} elseif ($fw == 0) {
if ($fh > 1) {
$fit = 0;
$w = floor($w / $fh);
$h = $height;
} else {
if ($fit) {
$width = $w;
}
}
} elseif (($fw < 1) and ($fh < 1)) {
//
} elseif ($fw > $fh) {
if ($fw >= 1) {
$w = $width;
$h = floor($h / $fw);
}
} elseif ($fh > $fw) {
if ($fh >= 1) {
$w = floor($w / $fh);
$h = $height;
}
}
if ($fit) {
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
$cw = $width;
$ch = $height;
} else {
$x = 0;
$y = 0;
$cw = $w;
$ch = $h;
}
$file2 = dirname($file) . '/' . $class . basename($file);
$f1 = 'imagecreatefrom' . $tag;
$src = $f1($file);
$new = imagecreatetruecolor($cw, $ch);
return self::create($new, $src, $file2, $x, $y, $w, $h, $cw, $ch, $a);
}
public static function crop($file, $class, $width='-', $height='-', $fit=0) {
if (!$class) exit('ExecImage: Original image can not be overwrite.');
$small = 0;
$a = getimagesize($file);
switch ($a[2]) {
case 1:
$tag = 'Gif';
break;
case 2:
$tag = 'Jpeg';
break;
case 3:
$tag = 'Png';
break;
default:
return;
}
$w = $a[0];
$h = $a[1];
if ($height == 0) {
//resize by width -- height will follow
$fh = 0;
$fw = $w / $width;
} elseif ($width == 0) {
//resize by height -- width will follow
$fw = 0;
$fh = $h / $height;
} else {
$fw = $w / $width;
$fh = $h / $height;
}
if (($fw <= 1) and ($fh <= 1)) {
$small = 1;
//dont resize
} else {
$fit = 1;
//chop by the smallest
if ($fh < $fw) {
//Crop By Height
$h = $height;
$w = floor($w / $fh);
//$w = $width;
//$h = floor($h /$fw);
//$w = $width;
} else {
//Crop By Width
$w = $width;
$h = floor($h /$fw);
//$h = $height;
//$w = floor($w / $fh);
}
}
$file2 = dirname($file) . '/' . $class . basename($file);
$f1 = 'ImageCreateFrom' . $tag;
$src = $f1($file);
if ($small) {
if ($fit) {
//image must be padded
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
} else {
//image goes as is -- shrinked
$x = 0;
$y = 0;
}
} else {
//image must be centered -- this should be a square from js
$x = ($width - $w) / 2;
$y = ($height - $h) / 2;
}
if ($small) {
if ($fit) {
//create with the full size
$new = imagecreatetruecolor($width, $height);
return self::create($new, $src, $file2, $x, $y, $w, $h, $width, $height, $a);
} else {
//nah, just with the original size
$new = imagecreatetruecolor($w, $h);
return self::create($new, $src, $file2, $x, $y, $w, $h, $w, $h, $a);
}
} else {
if ($fit) {
//create with the full size
$new = imagecreatetruecolor($width, $height);
return self::create($new, $src, $file2, $x, $y, $w, $h, $width, $height, $a);
} else {
//nah, just with the original size
$new = imagecreatetruecolor($w, $h);
return self::create($new, $src, $file2, $x, $y, $w, $h, $w, $h, $a);
}
}
}
private static function create($new, $src, $file2, $x, $y, $w, $h, $cw, $ch, $a) {
switch ($a[2]) {
case 1: // GIF
case 3: // PNG
// http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/
// http://www.mummey.org/2008/11/transparent-gifs-with-php-and-gd/
imagealphablending($new, false);
imagesavealpha($new, true);
$back = imagecolorallocatealpha($new, 255, 255, 255, 127);
imagefilledrectangle($new, 0, 0, $cw, $ch, $back);
imagecolortransparent($new, $back);
break;
case 2: // JPEG
$back = imagecolorallocate($new, 255, 255, 255);
break;
}
imagefill($new, 0, 0, $back);
imagecopyresampled($new, $src, $x, $y, 0, 0, $w, $h, $a[0], $a[1]);
if (file_exists($file2)) unlink($file2);
switch ($a[2]) {
case 1:
imagegif($new, $file2);
break;
case 2:
imagejpeg($new, $file2, 100);
break;
case 3:
imagepng($new, $file2, 0);
break;
}
imagedestroy($src);
imagedestroy($new);
return true;
}
}
So, any hints on how to preserve GIF transparency?
You put a link with your answer ;)
You're using imageCopyResampled which acts weird with alpha. Use imageCopyResized instead.
http://www.mummey.org/2008/11/transparent-gifs-with-php-and-gd/comment-page-1/#comment-94

Categories