I'm trying to display a captcha image using simple-php-captcha.php .It's working fine on my local but on the net it doesn't show the captcha image and I think it's because of my url structure.
The url of the page is like this http://test.org/v245/some-url and when I check the captcha image src , It's Look like this src="/simple-php-captcha.php?_CAPTCHA&t=0.11252600+1451561363".
I have tried this http://test.org/simple-php-captcha.php?_CAPTCHA&t=0.11252600+1451561363 But it doesn't make the captcha image .
What's the problem ? Should I change something in htaccess ?
UPDATE:
This is the code of simple-php-captcha.php:
<?php
//
// A simple PHP CAPTCHA script
//
// Copyright 2013 by Cory LaViska for A Beautiful Site, LLC
//
// See readme.md for usage, demo, and licensing info
//
function simple_php_captcha($config = array()) {
// Check for GD library
if( !function_exists('gd_info') ) {
throw new Exception('Required GD library is missing');
}
$bg_path = dirname(__FILE__) . '/backgrounds/';
$font_path = dirname(__FILE__) . '/fonts/';
// Default values
$captcha_config = array(
'code' => '',
'min_length' => 5,
'max_length' => 5,
'backgrounds' => array(
$bg_path . '45-degree-fabric.png',
$bg_path . 'cloth-alike.png',
$bg_path . 'grey-sandbag.png',
$bg_path . 'kinda-jean.png',
$bg_path . 'polyester-lite.png',
$bg_path . 'stitched-wool.png',
$bg_path . 'white-carbon.png',
$bg_path . 'white-wave.png'
),
'fonts' => array(
$font_path . 'times_new_yorker.ttf'
),
'characters' => 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnprstuvwxyz23456789',
'min_font_size' => 28,
'max_font_size' => 28,
'color' => '#666',
'angle_min' => 0,
'angle_max' => 10,
'shadow' => true,
'shadow_color' => '#fff',
'shadow_offset_x' => -1,
'shadow_offset_y' => 1
);
// Overwrite defaults with custom config values
if( is_array($config) ) {
foreach( $config as $key => $value ) $captcha_config[$key] = $value;
}
// Restrict certain values
if( $captcha_config['min_length'] < 1 ) $captcha_config['min_length'] = 1;
if( $captcha_config['angle_min'] < 0 ) $captcha_config['angle_min'] = 0;
if( $captcha_config['angle_max'] > 10 ) $captcha_config['angle_max'] = 10;
if( $captcha_config['angle_max'] < $captcha_config['angle_min'] ) $captcha_config['angle_max'] = $captcha_config['angle_min'];
if( $captcha_config['min_font_size'] < 10 ) $captcha_config['min_font_size'] = 10;
if( $captcha_config['max_font_size'] < $captcha_config['min_font_size'] ) $captcha_config['max_font_size'] = $captcha_config['min_font_size'];
// Use milliseconds instead of seconds
srand(microtime() * 100);
// Generate CAPTCHA code if not set by user
if( empty($captcha_config['code']) ) {
$captcha_config['code'] = '';
$length = rand($captcha_config['min_length'], $captcha_config['max_length']);
while( strlen($captcha_config['code']) < $length ) {
$captcha_config['code'] .= substr($captcha_config['characters'], rand() % (strlen($captcha_config['characters'])), 1);
}
}
// Generate HTML for image src
$image_src = substr(__FILE__, strlen( realpath($_SERVER['DOCUMENT_ROOT']) )) . '?_CAPTCHA&t=' . urlencode(microtime());
$image_src = '/' . ltrim(preg_replace('/\\\\/', '/', $image_src), '/');
$_SESSION['_CAPTCHA']['config'] = serialize($captcha_config);
return array(
'code' => $captcha_config['code'],
'image_src' => $image_src
);
}
if( !function_exists('hex2rgb') ) {
function hex2rgb($hex_str, $return_string = false, $separator = ',') {
$hex_str = preg_replace("/[^0-9A-Fa-f]/", '', $hex_str); // Gets a proper hex string
$rgb_array = array();
if( strlen($hex_str) == 6 ) {
$color_val = hexdec($hex_str);
$rgb_array['r'] = 0xFF & ($color_val >> 0x10);
$rgb_array['g'] = 0xFF & ($color_val >> 0x8);
$rgb_array['b'] = 0xFF & $color_val;
} elseif( strlen($hex_str) == 3 ) {
$rgb_array['r'] = hexdec(str_repeat(substr($hex_str, 0, 1), 2));
$rgb_array['g'] = hexdec(str_repeat(substr($hex_str, 1, 1), 2));
$rgb_array['b'] = hexdec(str_repeat(substr($hex_str, 2, 1), 2));
} else {
return false;
}
return $return_string ? implode($separator, $rgb_array) : $rgb_array;
}
}
// Draw the image
if( isset($_GET['_CAPTCHA']) ) {
session_start();
$captcha_config = unserialize($_SESSION['_CAPTCHA']['config']);
if( !$captcha_config ){
exit();
}
unset($_SESSION['_CAPTCHA']);
// Use milliseconds instead of seconds
srand(microtime() * 100);
// Pick random background, get info, and start captcha
$background = $captcha_config['backgrounds'][rand(0, count($captcha_config['backgrounds']) -1)];
list($bg_width, $bg_height, $bg_type, $bg_attr) = getimagesize($background);
$captcha = imagecreatefrompng($background);
$color = hex2rgb($captcha_config['color']);
$color = imagecolorallocate($captcha, $color['r'], $color['g'], $color['b']);
// Determine text angle
$angle = rand( $captcha_config['angle_min'], $captcha_config['angle_max'] ) * (rand(0, 1) == 1 ? -1 : 1);
// Select font randomly
$font = $captcha_config['fonts'][rand(0, count($captcha_config['fonts']) - 1)];
// Verify font file exists
if( !file_exists($font) ) throw new Exception('Font file not found: ' . $font);
//Set the font size.
$font_size = rand($captcha_config['min_font_size'], $captcha_config['max_font_size']);
$text_box_size = imagettfbbox($font_size, $angle, $font, $captcha_config['code']);
// Determine text position
$box_width = abs($text_box_size[6] - $text_box_size[2]);
$box_height = abs($text_box_size[5] - $text_box_size[1]);
$text_pos_x_min = 0;
$text_pos_x_max = ($bg_width) - ($box_width);
$text_pos_x = rand($text_pos_x_min, $text_pos_x_max);
$text_pos_y_min = $box_height;
$text_pos_y_max = ($bg_height) - ($box_height / 2);
$text_pos_y = rand($text_pos_y_min, $text_pos_y_max);
// Draw shadow
if( $captcha_config['shadow'] ){
$shadow_color = hex2rgb($captcha_config['shadow_color']);
$shadow_color = imagecolorallocate($captcha, $shadow_color['r'], $shadow_color['g'], $shadow_color['b']);
imagettftext($captcha, $font_size, $angle, $text_pos_x + $captcha_config['shadow_offset_x'], $text_pos_y + $captcha_config['shadow_offset_y'], $shadow_color, $font, $captcha_config['code']);
}
// Draw text
imagettftext($captcha, $font_size, $angle, $text_pos_x, $text_pos_y, $color, $font, $captcha_config['code']);
// Output image
header("Content-type: image/png");
imagepng($captcha);
}
Thanks
Related
I'm currently implementing a function that generated an image I can use to print it on a PDF file with FPDI. During the creation of the image from the memory, I'm getting this error:
PHP Warning: getimagesize(var://imgd41d8cd98f00b204e9800998ecf8427e):
failed to open stream: "VariableStream::stream_open" call
failed in /Applications/MAMP/htdocs/fpdi/src/fpdi_extended.php on line
77
I don't get it anymore. This is my code:
<?php
require_once 'autoload.php';
use setasign\Fpdi\Fpdi;
class VariableStream {
private $varname;
private $position;
public function streams_open( $path, $mode, $options, &$opened_path ): bool {
$url = parse_url( $path );
$this->varname = $url['host'];
if ( ! isset( $GLOBALS[ $this->varname ] ) ) {
header( 'HTTP/1.1 500 Internal Server Error' );
header( 'Content-Type: application/json; charset=UTF-8' );
die( json_encode( array(
'message' => 'Global variable ' . $this->varname . ' does not exist',
'code' => 1337
) ) );
}
$this->position = 0;
return true;
}
public function stream_read( $count ) {
$ret = substr( $GLOBALS[ $this->varname ], $this->position, $count );
$this->position += strlen( $ret );
return $ret;
}
public function stream_eof(): bool {
return $this->position >= strlen( $GLOBALS[ $this->varname ] );
}
public function stream_tell() {
return $this->position;
}
public function stream_seek( $offset, $whence ): bool {
if ( $whence === SEEK_SET ) {
$this->position = $offset;
return true;
}
return false;
}
public function stream_stat(): array {
return array();
}
}
class Fpdi_Extended extends Fpdi {
public function __construct( $orientation = 'P', $unit = 'mm', $format = 'A4' ) {
parent::__construct( $orientation, $unit, $format );
// Register var stream protocol
stream_wrapper_register( 'var', 'VariableStream' );
}
public function MemImage( $data, $x = null, $y = null, $w = 0, $h = 0, $link = '' ): void {
// Display the image contained in $data
$v = 'img' . md5( $data );
$GLOBALS[ $v ] = $data;
$a = getimagesize( 'var://' . $v );
if ( ! $a ) {
header( 'HTTP/1.1 500 Internal Server Error' );
header( 'Content-Type: application/json; charset=UTF-8' );
die( json_encode( array( 'message' => 'ERROR: Invalid signature data.', 'code' => 1337 ) ) );
}
$type = substr( strstr( $a['mime'], '/' ), 1 );
$this->Image( 'var://' . $v, $x, $y, $w, $h, $type, $link );
unset( $GLOBALS[ $v ] );
}
public function GDImage( $im, $x = null, $y = null, $w = 0, $h = 0, $link = '' ): void {
// Display the GD image associated with $im
ob_start();
imagecreatefromstring( base64_decode( preg_replace( '#^data:image/\w+;base64,#i', '', $im ) ) );
$data = ob_get_clean();
$this->MemImage( $data, $x, $y, $w, $h, $link );
}
}
I'm calling the code this way:
require_once 'fpdf/fpdf.php';
require_once 'fpdi/src/fpdi_extended.php';
$pdf = new Fpdi_Extended();
$page_counter = $pdf->setSourceFile( 'template.pdf' );
$tplIdx = $pdf->importPage( 5 );
$pdf->AddPage();
$pdf->useTemplate( $tplIdx, 10, 10, 100 );
$pdf->GDImage( $signature );
imagedestroy( $signature );
$pdf->Output();
After fixing the typo issue streams_open -> stream_open, I'm hanging here:
PHP Notice: getimagesize(): Read error! in
/Applications/MAMP/htdocs/fpdi/src/fpdi_extended.php
An example value for $signature is: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnYAAACWCAYAAABTj8VSAAAMcElEQVR4Xu3cOYtsaxUG4HWd5+s8gQMIgoJDIigoiImZmZmxIIixuaGIgeDwEzQwvpkaiCaKICiIiCMqzvM88ML3wabpvj3c7j6n1no2HKq6elfVfp+1u+s9e1f1I2UhQIAAAQIECBBoIfBIixRCECBAgAABAgQIlGJnJyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAgQIECAgGJnHyBAgAABAgQINBFQ7JoMUgwCBAg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AgMAgAcVu0LBFJUCAAAECBHoLKHa95ysdAQIECBAgMEhAsRs0bFEJECBAgACB3gKKXe/5SkeAAAECBAgMElDsBg1bVAIECBAgQKC3gGLXe77SESBAgAABAoMEFLtBwxaVAAECBAgQ6C2g2PWer3QECBAgQIDAIAHFbtCwRSVAgAABAgR6Cyh2vecrHQECBAgQIDBIQLEbNGxRCRAgQIAAgd4Cil3v+UpHgAABAgQIDBJQ7AYNW1QCBAgQIECgt4Bi13u+0hEgQIAAAQKDBBS7QcMWlQABAgQIEOgtoNj1nq90BAgQIECAwCCB/wNQCMaXtfRpgwAAAABJRU5ErkJggg==
Not able to install the FPDI package at the moment but I think you need to convert the image to a PNG before reading it from memory. Try editing your GDImage function like so:
public function GDImage( $im, $x = null, $y = null, $w = 0, $h = 0, $link = '' ): void {
ob_start();
$resource = imagecreatefromstring( base64_decode( preg_replace( '#^data:image/\w+;base64,#i', '', $im ) ) );
imagepng($resource);
$data = ob_get_clean();
$this->MemImage( $data, $x, $y, $w, $h, $link );
}
With the help of waterloomatt I've found my solution. I've changed the function below:
public function GDImage( $im, $x = null, $y = null, $w = 0, $h = 0, $link = '' ): void {
// Display the GD image associated with $im
$resource = imagecreatefromstring( base64_decode( preg_replace( '#^data:image/\w+;base64,#i', '', $im ) ) );
imagesavealpha( $resource, true );
ob_start();
imagepng( $resource );
$data = ob_get_clean();
imagedestroy( $resource );
$this->MemImage( $data, $x, $y, $w, $h, $link );
}
First, I was confused because the save base64 image on my PDF was completely black. So I've informed me about the used methods. So I came across this function here which saves some color informations in the resource:
imagesavealpha( $resource, true );
Now it's working great. Thanks to waterloomatt for the time!
what i'm trying to do here, in my php code below i have to set file name manually and i want to make it some how it grabds the file name automatically but without file extention
here's part of my code where i want to get file name
$Pages = array(
'clothes' => 'Clothes',
'shirt' => 'shirt',
'this-shirt' => 'This Shirt'
);
where it says "this-shirt" is file name and i want it to be set automatically instead of i write it down everytime i create a page. also here's full code
<?php
$Pages = array(
'clothes' => 'Clothes',
'shirt' => 'shirt',
'this-shirt' => 'This Shirt'
);
$path = $_SERVER["PHP_SELF"];
$parts = explode('/', $path);
if (count($parts) < 2) {
echo("home");
} else {
echo ("Home » ");
for ($i = 2; $i < count($parts); $i++) {
if (!strstr($parts[$i], ".")) {
echo("<a href=\"");
for ($j = 0; $j <= $i; $j++) {
echo $parts[$j] . "/";
};
echo("\">" . str_replace('-', ' ', $Pages[$parts[$i]]) . "</a> » ");
} else {
$str = $parts[$i];
$pos = strrpos($str, ".");
$parts[$i] = substr($str, 0, $pos);
echo str_replace('-', ' ', $Pages[$parts[$i]]);
}
}
}
hope you get the idea. thanks
This should do it:
// get this-shirt.php from the URL
$file = basename($_SERVER["PHP_SELF"]);
// pure magic
$filename = (count(explode('.', $file)) === 1 ? $file : implode('.', array_slice(explode('.', $file), 0, (count(explode('.', $file))-1))));
$Pages = array(
'clothes' => 'Clothes',
'shirt' => 'shirt',
$filename => 'This Shirt' // use $filename to declare the array's key
);
Hello i have images gotten from the database, after applying image convolution to them i am trying to output the image into an html tag. how do i go around this, have been trying to rack my brain for 3 days now no result looked at all the post i could find still no way to go around this
$eyt = pathinfo($mname, PATHINFO_EXTENSION); //get image extension
if($eyt=='jpg' || $eyt=='jpeg' || $eyt=='JPG' || $eyt=='JPEG')
{
$sr=imagecreatefromjpeg('media/'.$mname.'');
}
if($eyt=='png' || $eyt=='PNG')
{
$sr=imagecreatefrompng('media/'.$mname.'');
}
if($eyt=='gif' || $eyt=='GIF')
{
$sr=imagecreatefromgif('media/'.$mname.'');
}
$matrix = array(
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1),
);
$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0;
imageconvolution($sr, $matrix, $divisor, $offset);
//output what i tried took the output code from a stackoverflow post tried to edit it to my format but no result
if($eyt=='jpg' || $eyt=='jpeg' || $eyt=='JPG' || $eyt=='JPEG')
{
ob_start();
imagejpeg($sr);
$raw = ob_get_clean();
$er= 'data:image/jpg;base64,' . base64_encode( $raw ) . '';
}
if($eyt=='png' || $eyt=='PNG')
{
ob_start();
imagepng($sr);
$raw = ob_get_clean();
$er= 'data:image/png;base64,' . base64_encode( $raw ) . '';
}
if($eyt=='gif' || $eyt=='GIF')
{
ob_start();
imagegif($sr);
$raw = ob_get_clean();
$er= 'data:image/gif;base64,' . base64_encode( $raw ) . '';
}
<img class="img-responsive pad tyi abn" src="media/<?php echo $er ?>" >
When i check my source code this is what i get
<img src="media/data:image/jpg;base64," . base64_encode( $raw ) . " >
I have a piece of php code that exports data from mysql to excel. even using
utf-8 for charset, Persian(arabic) are saved weird and they are not recognizable.
If you can help me I will be grateful.
<?php
$con=mysqli_connect("localhost","root","","myDB");
$SQL = mysqli_query($con,"SELECT * FROM view1");
$header = '';
$result ='';
$exportData = mysql_query ($SQL ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $exportData );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $exportData , $i ) . "\t";
}
while( $row = mysql_fetch_row( $exportData ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$result .= trim( $line ) . "\n";
}
$result = str_replace( "\r" , "" , $result );
if ( $result == "" )
{
$result = "\nNo Record(s) Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$result";
?>
Php native support for this is not that good.
Have you tried php-gd-farsi?
include('php-gd-farsi-master/FarsiGD.php');
$gd = new FarsiGD();
// Create a 300x100 image
$im = imagecreatetruecolor(300, 100);
$red = imagecolorallocate($im, 0xFF, 0x00, 0x00);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
// Make the background red
imagefilledrectangle($im, 0, 0, 299, 99, $red);
// Path to our ttf font file
//$font_file = './Vera.ttf';
$font_file = './cour.ttf';
// Draw the text 'PHP Manual' using font size 13
$text = imagecreatetruecolor(200, 60);
imagefilledrectangle($text, 0, 0, 200, 60, $red);
$str = '**ماه**';
$tx = $gd->persianText($str, 'fa', 'normal');
imagefttext($text, 24, 10, 10, 50, $black, $font_file,$tx );
$im = $text;
// Output image to the browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
I'm using this following function to check if images exist at their location. Each time the script runs it load about 40 - 50 urls and so its taking long time to load the page. I was thinking of using threading for the "for statement" (at the end of the script) but couldn't find many examples on how to do that. I'm not very familiar with multi-threading with php but i found an example here using popen.
My script:
function get_image_dim($sURL) {
try {
$hSock = # fopen($sURL, 'rb');
if ($hSock) {
while(!feof($hSock)) {
$vData = fread($hSock, 300);
break;
}
fclose($hSock);
if (strpos(' ' . $vData, 'JFIF')>0) {
$vData = substr($vData, 0, 300);
$asResult = unpack('H*',$vData);
$sBytes = $asResult[1];
$width = 0;
$height = 0;
$hex_width = '';
$hex_height = '';
if (strstr($sBytes, 'ffc2')) {
$hex_height = substr($sBytes, strpos($sBytes, 'ffc2') + 10, 4);
$hex_width = substr($sBytes, strpos($sBytes, 'ffc2') + 14, 4);
} else {
$hex_height = substr($sBytes, strpos($sBytes, 'ffc0') + 10, 4);
$hex_width = substr($sBytes, strpos($sBytes, 'ffc0') + 14, 4);
}
$width = hexdec($hex_width);
$height = hexdec($hex_height);
return array('width' => $width, 'height' => $height);
} elseif (strpos(' ' . $vData, 'GIF')>0) {
$vData = substr($vData, 0, 300);
$asResult = unpack('h*',$vData);
$sBytes = $asResult[1];
$sBytesH = substr($sBytes, 16, 4);
$height = hexdec(strrev($sBytesH));
$sBytesW = substr($sBytes, 12, 4);
$width = hexdec(strrev($sBytesW));
return array('width' => $width, 'height' => $height);
} elseif (strpos(' ' . $vData, 'PNG')>0) {
$vDataH = substr($vData, 22, 4);
$asResult = unpack('n',$vDataH);
$height = $asResult[1];
$vDataW = substr($vData, 18, 4);
$asResult = unpack('n',$vDataW);
$width = $asResult[1];
return array('width' => $width, 'height' => $height);
}
}
} catch (Exception $e) {}
return FALSE;
}
for($y=0;$y<= ($image_count-1);$y++){
$dim = get_image_dim($images[$y]);
if (empty($dim)) {
echo $images[$y];
unset($images[$y]);
}
}
$images = array_values($images);
The popen example i found was:
for ($i=0; $i<10; $i++) {
// open ten processes
for ($j=0; $j<10; $j++) {
$pipe[$j] = popen('script.php', 'w');
}
// wait for them to finish
for ($j=0; $j<10; ++$j) {
pclose($pipe[$j]);
}
}
I'm not sure which part of my code has to go in the script.php? I tried moving the whole script but that didn't work?
Any ideas on how can i implement this or if there is a better way to multi thread it? Thanks.
PHP does not have multi-threading natively. You can do it with pthreads, but having a little experience there, I can say with assurance that that is too much for your needs.
Your best bet will be to use curl, you can initiate multiple requests with curl_multi_init. Based off the example on PHP.net, the following may work for your needs:
function curl_multi_callback(Array $urls, $callback, $cache_dir = NULL, $age = 600) {
$return = array();
$conn = array();
$max_age = time()-intval($age);
$mh = curl_multi_init();
if(is_dir($cache_dir)) {
foreach($urls as $i => $url) {
$cache_path = $cache_dir.DIRECTORY_SEPARATOR.sha1($url).'.ser';
if(file_exists($cache_path)) {
$stat = stat($cache_path);
if($stat['atime'] > $max_age) {
$return[$i] = unserialize(file_get_contents($cache_path));
unset($urls[$i]);
} else {
unlink($cache_path);
}
}
}
}
foreach ($urls as $i => $url) {
$conn[$i] = curl_init($url);
curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $conn[$i]);
}
do {
$status = curl_multi_exec($mh, $active);
// Keep attempting to get info so long as we get info
while (($info = curl_multi_info_read($mh)) !== FALSE) {
// We received information from Multi
if (false !== $info) {
// The connection was successful
$handle = $info['handle'];
// Find the index of the connection in `conn`
$i = array_search($handle, $conn);
if($info['result'] === CURLE_OK) {
// If we found an index and that index is set in the `urls` array
if(false !== $i && isset($urls[$i])) {
$content = curl_multi_getcontent($handle);
$return[$i] = $data = array(
'url' => $urls[$i],
'content' => $content,
'parsed' => call_user_func($callback, $content, $urls[$i]),
);
if(is_dir($cache_dir)) {
file_put_contents($cache_dir.DIRECTORY_SEPARATOR.sha1($urls[$i]).'.ser', serialize($data));
}
}
} else {
// Handle failures how you will
}
// Close, even if a failure
curl_multi_remove_handle($mh, $handle);
unset($conn[$i]);
}
}
} while ($status === CURLM_CALL_MULTI_PERFORM || $active);
// Cleanup and resolve any remaining connections (unlikely)
if(!empty($conn)) {
foreach ($conn as $i => $handle) {
if(isset($urls[$i])) {
$content = curl_multi_getcontent($handle);
$return[$i] = $data = array(
'url' => $urls[$i],
'content' => $content,
'parsed' => call_user_func($callback, $content, $urls[$i]),
);
if(is_dir($cache_dir)) {
file_put_contents($cache_dir.DIRECTORY_SEPARATOR.sha1($urls[$i]).'.ser', serialize($data));
}
}
curl_multi_remove_handle($mh, $handle);
unset($conn[$i]);
}
}
curl_multi_close($mh);
return $return;
}
$return = curl_multi_callback($urls, function($data, $url) {
echo "got $url\n";
return array('some stuff');
}, '/tmp', 30);
//print_r($return);
/*
$url_dims = array(
'url' => 'http://www......',
'content' => raw content
'parsed' => return of get_image_dim
)
*/
Just restructure your original function get_image_dim to consume the raw data and output whatever you are looking for.
This is not a complete function, there may be errors, or idiosyncrasies you need to resolve, but it should serve as a good starting point.
Updated to include caching. This changed a test I was running on 18 URLS from 1 second, to .007 seconds (with cache hits).
Note: you may want to not cache the full request contents, as I did, and just cache the url and the parsed data.