Forum Moderators: coopster

Message Too Old, No Replies

Image handling

imagecreatefromjpeg imagecreatefromgif

         

nathan1702

4:21 pm on Jan 16, 2015 (gmt 0)

10+ Year Member



I have the script below and hope somebody can help with it,it handles the jpg ok but not the gif.

<?php 
ini_set('display_errors',0);
//error_reporting(E_ALL);

$max_width = 0;
$mam = isset($_GET['mam']) ? true : false;
$img = $_GET['img'];
$supplier_id = @$_GET['s'];

if($img == "no_image"){
switch( @$_GET['size'] )
{
case "small":
$type = "image/jpeg";
$filename = $_GET['img'];
$split = explode(".", $filename);
$filename = $split[0] ."_small";
break;
case "list":
$type = "image/jpeg";
$filename = $_GET['img'];
$split = explode(".", $filename);
$filename = $split[0] ."_list";
break;
case "detail":
$type = "image/jpeg";
$filename = $_GET['img'];
$split = explode(".", $filename);
$filename = $split[0] ."_detail";
break;
case "email":
$type = "image/jpeg";
$filename = $_GET['img'];
$split = explode(".", $filename);
$filename = $split[0] ."_email";
break;
}
$show_watermark = false;
$image = "part_images/" . $filename .".jpg";

}else{
if(!$mam){
$type = "image/jpeg";
switch( @$_GET['size'] )
{
case "small":
$show_watermark = false;
$max_width = 60;
$max_height = 60;
$image = !is_null($supplier_id) ? "part_images/". $supplier_id ."/". $img .".jpg" : "part_images/". $img;
break;
case "list":
$show_watermark = false;
$max_width = 160;
$max_height = 160;
$image = !is_null($supplier_id) ? "part_images/". $supplier_id ."/". $img .".jpg" : "part_images/". $img;
break;
case "detail":
$show_watermark = false;
$watermark = "images/watermark.png";
$max_width = 220;
$max_height = 220;
$image = !is_null($supplier_id) ? "part_images/". $supplier_id ."/". $img .".jpg" : "part_images/". $img;
break;
case "email":
$show_watermark = false;
$watermark = "images/watermark.png";
$max_width = 110;
$max_height = 110;
$image = !is_null($supplier_id) ? "part_images/". $supplier_id ."/". $img .".jpg" : "part_images/". $img;
break;
case "large":
$show_watermark = false;
$watermark = "images/watermark_large.png";
$max_width = 600;
$max_height = 600;
$image = !is_null($supplier_id) ? "part_images/". $supplier_id ."/". $img .".jpg" : "part_images/". $img;
break;
}

}else{

$filename = str_replace("__", "/", $img);
$ext = @strtolower($_GET['ext']);
$type = $ext=="jpg" ? "image/jpeg" : "image/gif";
$image_url = "http://www.example/acweb/images/";

switch( @$_GET['size'] )
{
case "small":
$show_watermark = false;
$max_width = 60;
$max_height = 60;
break;
case "list":
$show_watermark = false;
$max_width = 150;
$max_height = 150;
break;
case "detail":
$show_watermark = false;
$watermark = "images/watermark.png";
$max_width = 220;
$max_height = 220;
break;
case "multi":
$show_watermark = false;
$watermark = "images/watermark.png";
$max_width = 700;
$max_height = 240;
break;
case "email":
$show_watermark = false;
$watermark = "images/watermark.png";
$max_width = 110;
$max_height = 110;
break;
case "large":
$show_watermark = false;
$watermark = "images/watermark_large.png";
$max_width = 600;
$max_height = 600;
break;
}

$image = $image_url . $filename .".". $ext;
}

}
if($max_width>0){
//create image so we can do the resize
if($type=="image/jpeg"){
if(!$imgTmp = imagecreatefromjpeg($image)){
die();
}
}else{
if(!$imgTmp = imagecreatefromgif($image)){
die();
}
}

//get orginial file properties
list($orginialWidth, $orginialHeight) = getimagesize($image);


if($orginialWidth > $max_width || $orginialHeight > $max_height){
$deltaWidth = $orginialWidth - $max_width;
$deltaHeight = $orginialHeight - $max_height;
if($deltaHeight > $deltaWidth){
//scale by height
$scaleFactor = $max_height / $orginialHeight;
}else{
//scale by width
$scaleFactor = $max_width / $orginialWidth;
}

$newWidth = $orginialWidth * $scaleFactor;
$newHeight = $orginialHeight * $scaleFactor;
}else{
$newWidth = $orginialWidth;
$newHeight = $orginialHeight;
}
if($_GET['size']=="detail"){
$im2 = imagecreatetruecolor($max_width , $max_height);
}else{
$im2 = imagecreatetruecolor($max_width , $max_height);
}
imagefill($im2, 1, 1, imagecolorallocate($im2, 255, 255, 255));

//now resize the image
imagecopyresampled($im2, $imgTmp, (imagesx($im2)/2)-($newWidth/2), (imagesy($im2)/2)-($newHeight/2), 0 ,0, $newWidth, $newHeight, $orginialWidth, $orginialHeight);
imagedestroy($imgTmp);
}else{
if($type=="image/jpeg"){
$im2 = imagecreatefromjpeg($image);
}else{
$im2 = imagecreatefromgif($image);
}
}

if($show_watermark){
$im = imagecreatefrompng($watermark);
imagecopy($im2, $im, (imagesx($im2)/2)-(imagesx($im)/2), (imagesy($im2)/2)-(imagesy($im)/2), 0, 0, imagesx($im), imagesy($im));
}

imageinterlace($im2, 1);
header("Content-Type: $type");
if($type=="image/jpeg"){
imagejpeg($im2,NULL, 100);
}else{
imagegif($im2);
}
echo $image;
//imagedestroy($im);
imagedestroy($im2);
?>

lammert

8:17 am on Jan 17, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi Nathan1702,

A large bunch of code but no description of your problem. Can you only post the piece of code where you think the problem is, and give a thorough description of what you expect the code to do, and what you observe that it does?

nathan1702

10:26 am on Jan 17, 2015 (gmt 0)

10+ Year Member



Hi, yes sorry. I have this code which takes an image and resizes the image for display on a web page, trouble is for some reason that I cannot spot the script will return the jpg files but not gif files. Thanks for replying

if($max_width>0){
//create image so we can do the resize
if($type=="image/jpeg"){
if(!$imgTmp = imagecreatefromjpeg($image)){
die();
}
}else{
if(!$imgTmp = imagecreatefromgif($image)){
die();
}
}

//get orginial file properties
list($orginialWidth, $orginialHeight) = getimagesize($image);


if($orginialWidth > $max_width || $orginialHeight > $max_height){
$deltaWidth = $orginialWidth - $max_width;
$deltaHeight = $orginialHeight - $max_height;
if($deltaHeight > $deltaWidth){
//scale by height
$scaleFactor = $max_height / $orginialHeight;
}else{
//scale by width
$scaleFactor = $max_width / $orginialWidth;
}

$newWidth = $orginialWidth * $scaleFactor;
$newHeight = $orginialHeight * $scaleFactor;
}else{
$newWidth = $orginialWidth;
$newHeight = $orginialHeight;
}
if($_GET['size']=="detail"){
$im2 = imagecreatetruecolor($max_width , $max_height);
}else{
$im2 = imagecreatetruecolor($max_width , $max_height);
}
imagefill($im2, 1, 1, imagecolorallocate($im2, 255, 255, 255));

//now resize the image
imagecopyresampled($im2, $imgTmp, (imagesx($im2)/2)-($newWidth/2), (imagesy($im2)/2)-($newHeight/2), 0 ,0, $newWidth, $newHeight, $orginialWidth, $orginialHeight);
imagedestroy($imgTmp);
}else{
if($type=="image/jpeg"){
$im2 = imagecreatefromjpeg($image);
}else{
$im2 = imagecreatefromgif($image);
}
}

if($show_watermark){
$im = imagecreatefrompng($watermark);
imagecopy($im2, $im, (imagesx($im2)/2)-(imagesx($im)/2), (imagesy($im2)/2)-(imagesy($im)/2), 0, 0, imagesx($im), imagesy($im));
}

imageinterlace($im2, 1);
header("Content-Type: $type");
if($type=="image/jpeg"){
imagejpeg($im2,NULL, 100);
}else{
imagegif($im2);
}
echo $image;
//imagedestroy($im);
imagedestroy($im2)

nathan1702

2:22 pm on Jan 17, 2015 (gmt 0)

10+ Year Member



this is on php 5.3

penders

7:07 pm on Jan 17, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



ini_set('display_errors',0);
//error_reporting(E_ALL);


What errors are you getting? This snippet, from the very top of your script, would perhaps suggest you aren't actually seeing "anything"? As a start, you need to enable full error reporting (E_ALL | E_STRICT on PHP 5.3) and perhaps display_errors (1) as well (unless you are logging such errors instead?).

And avoid error suppression (@), it makes debugging that much harder. (In fact, the error suppression at the top of your script could be masking runtime errors.)

Is this intentional (repeated many times)...
$image = !is_null($supplier_id) ? "part_images/". $supplier_id ."/". $img .".jpg" : "part_images/". $img;


You aren't appending a file extension if the $supplier_id is null?

nathan1702

7:52 pm on Jan 17, 2015 (gmt 0)

10+ Year Member



I have just solved this problem. The server does not seem o be allowing imagecreate from gif so I changed it to imagecreate from png even though the image is a gif. Hope this helps anyone else having the problem