M
M
maximus2016-04-27 16:00:43
PHP
maximus, 2016-04-27 16:00:43

How to create a valid png using php?

You need to get data from the webcam, save it to a temporary png file.
Code below:

<?
ini_set('display_errors','Off');
ini_set('log_errors', 'On');
ini_set('error_log', 'ipcam.php.log');

$rand = rand(1000,9999);
$url = 'http://cam.zmk26.ru:80/snapshot.cgi?'.$rand;
$cacheFile = 'ipcam_cam.jpg';

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_URL,$url); 
curl_setopt($curl_handle, CURLOPT_USERPWD, "guest:guest");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

// Выводим если нет соединения с камерой
if (empty($buffer)) {
  header("Content-Type: image/jpeg");
  readfile($cacheFile);
}
// Если изображение не получилось загрузить
elseif($buffer == "Не могу получить изображение.") {
  print "Не могу получить изображение.";
}
else 
{
  // Проверяем на наличие файла. Если его нет загружаем или старее 15 минут обновляем.
  if (!(file_exists($cacheFile)) or (time() - filemtime($cacheFile) > 60 * 15)) {
    
    header("Content-Type: image/jpeg");
    file_put_contents($cacheFile, $buffer);
    print $buffer;
    
  }
  else {
    header("Content-Type: image/jpeg");
    readfile($cacheFile);
  }
}
?>

Everything seems to be good, but then when I do imagecreatefrompng to overlay text on this image, it writes to the log "is not a valid PNG file", which it cannot create. Imagecreatefromjpeg said "gd-jpeg: JPEG library reports unrecoverable error:".
image.php:
<?
header("Content-type: image/jpeg");
  
ini_set('date.timezone', 'Europe/Moscow');
putenv('GDFONTPATH=' . realpath('.'));
ini_set('display_errors','Off');
ini_set('log_errors', 'On');
ini_set('error_log', 'image.php.log');
error_reporting(E_ALL ^E_NOTICE | E_STRICT);

function rus_date() {
  $translate = array(
  "Monday"	=> "Понедельник",
  "Tuesday"	=> "Вторник",
  "Wednesday" => "Среда",
  "Thursday"	=> "Четверг",
  "Friday"	=> "Пятница",
  "Saturday"	=> "Суббота",
  "Sunday"	=> "Воскресенье",
  "January"	=> "января",
  "February"	=> "февраля",
  "March" 	=> "марта",
  "April" 	=> "апреля",
  "May"		=> "мая",
  "June"		=> "июня",
  "July"		=> "июля",
  "August"	=> "августа",
  "September"	=> "сентября",
  "October"	=> "октября",
  "November"	=> "ноября",
  "December"	=> "декабря"
  );
  
  if (func_num_args() > 1) {
    $timestamp = func_get_arg(1);
    return strtr(date(func_get_arg(0), $timestamp), $translate);
  } else {
    return strtr(date(func_get_arg(0)), $translate);
  }
}

$camdir = $_SERVER['DOCUMENT_ROOT']."/webcam/";
$src = imagecreatefromjpeg($camdir."ipcam.php");
//$src = imagecreatefromjpeg("ipcam_cam.jpg");

$font_size = "18";
$white = imagecolorallocate($src, 255, 255, 255);
$color = imagecolorallocate($src, 12, 61, 123); //blue
$color2 = imagecolorallocate($src, 204, 2, 0); //red
$font_face = "futurademic";
$text = "ЗАО «Завод металлоконструкций»";

$time = rus_date("l, j F Y G:i");/*date('d M Y G:i:s');*/

imagettftext($src, 36, 0, 50, 650, $white, $font_face, "zmk26.ru");
imagettftext($src, $font_size, 0, 50, 680, $white, $font_face, $text);

imagefilledrectangle($src,0,60,150,115,$color2);
imagettftext($src, 24, 0, 50, 100, $white, $font_face, "LIVE");
imagettftext($src, 24, 0, 180, 100, $white, $font_face, $time); // время

imagejpeg($src, NULL, 100);
imagedestroy($src);

?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2016-04-27
@alsopub

You open the source code of the script, of course there will be no png.
imagecreatefrompng("ipcam.php"); replace with imagecreatefrompng('ipcam_cam.png');
Or, separate the functions for working with the ip camera into a separate script, connect it here and there and call them as needed.

Y
Yuri, 2016-04-27
@riky

I was not too lazy, logged in, looked at your factory ..
the picture is there in jpeg format
, you need to use imagecreatefromjpeg to download

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question