Tag Archives: php

PHP: Fatal error: call to undefined function iconv()

Make sure it’s enabled in php.ini. 🙂

I also had to restart Apache.

Advertisements

PHP doesn’t execute scripts

Neither using CLI (with -f or a shebang) nor through Apache.

It was just dumping the script to stdout, and that means short PHP tags (<? ... ?>) are disabled. Nothing serious, fixed in php.ini.

But then it just did nothing. No output, no errors, no warnings. Turned out this particular version of PHP can’t read DOS-formatted text files. To change them to Unix, in vim:

:set ff-unix

This happened after I flipped from FreeBSD to Arch Linux.

Batch image resizing

My php script no longer works, it breaks on gd functions somehow.

So I use this Gimp script instead.

A PHP script to resize all images in a directory

This is a crude hack to resize images in a directory. The script is invoked as:

shrink <new_x> <new_y> <file> <file> <file> ...

You’ll need PHP with the GD library. That’s php5-gd on FreeBSD.


#!/usr/local/bin/php
# The output is JPEG, quality 88.

<?php



// Fuction to resize images, originally by D. Weimer.
// Code reformatting done, and '>' changed to '<'.
// http://www.dweimer.net/?page=help&subpage=imageresize
function resize($img,$nx,$ny)
{
    // $x and $y: original image's sizes.
    $x = imagesx($img);
    $y = imagesy($img);

    // Here, $nx and $ny are the minimal desired dimensions.
    if ( ($x/$nx) < ($y/$ny) )
    {
        $ny = round( $y * ($nx/$x) );
    }
    else
    {
        $nx = round( $x * ($ny/$y) );
    }

    $nimg = imagecreatetruecolor($nx,$ny);
    imagecopyresampled($nimg,$img,0,0,0,0,$nx,$ny,$x,$y);
    imagedestroy($img);
    return($nimg);
}



$arguments = $_SERVER['argv'];          // What the user wants to process.
array_shift($arguments);                // 1st element is the path to the script itself.
$newx = array_shift($arguments);        // 2nd is the new desired x dimension.
$newy = array_shift($arguments);        // 3rd is new y.
                                        // all the remaining args are images.

foreach ( $arguments as $arg )          // Go over user's shown items (single or "heaps").
{
    foreach (glob("$arg") as $filename) // Go over every file in a "heap".
    {
        $image = file_get_contents($filename);
        $image = imagecreatefromstring($image);

        $image = resize($image, $newx, $newy);

        imagejpeg( $image, $filename, 88 ); // WARNING!!! Writes JPEG by default!
    }
}


?>

I was too lazy to go searching for something pre-made.

RDF to iCal converter (PHP script)

…which can easily be adapted to, say, RSS or shit.

I wrote this with a particular RDF feed in mind, so you might have to tweak it, if you want to use it.

Also, see the TODOs.

<?php

// GPLv3.
// Parsing part copied over from PHP manual.
// Also used RFC2445: http://www.ietf.org/rfc/rfc2445.txt
// Distributed under fuck you license.

class RDFItem {
	var $link;		//
	var $title;		//
	var $description;	//
	var $date;		//
//	var $dc:date;		// hell knows what this is

    function RDFItem ($link)
    {
        foreach ( $link as $k => $v )
            $this->$k = $link[$k];
    }
}

function readRDF( $filename )
{
    // read the RDF
    $data = implode("", file($filename));
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $data, $values, $tags);
    xml_parser_free($parser);

    // loop through the file structures
    foreach ($tags as $key=>$val) {
        if ($key == 'item') {
            $itemranges = $val;

            // each contiguous pair of array entries are the
            // lower and upper range for each RDF feed item
            // (what?..)

            for ($i=0; $i < count($itemranges); $i+=2) {
                $offset = $itemranges[$i] + 1;
                $len = $itemranges[$i + 1] - $offset;
                $tdb[] = parseItem( array_slice($values, $offset, $len) );
            }
        } else {
            continue;
        }
    }

    return $tdb;
}

function parseItem($subElements)
{
	for ( $i = 0; $i \n/", "", $desc, 1 );

	// Make sure all lines except the 1st have a space up front.
	$desc = str_replace( "\n", "", $desc );
	$desc = str_replace( "<br />", "\\n\n ", $desc );
	$desc = str_replace( "<br/>", "\\n\n ", $desc );

	// Manage links.
	$desc = preg_replace( "/<a href=\"(.*?)\">/s", " [$1] ", $desc );
	$desc = str_replace( "</a>", "", $desc );

	// Manage all other HTML tags.
//	$desc = preg_replace( "/<(?:.*?)>/s", "", $desc );

	// TODO: check that lines are folded at 75 octet.

	return $desc;
}


$db = readRDF( 'hardcore.rdf' );

/*
echo "<pre>";
print_r($db);
echo "</pre>";
*/




// Now for the dirty punk solution.
// Create the calendar.
$cal  = "BEGIN:VCALENDAR\nVERSION:2.0\n";
$cal .= "X-WR-CALNAME:Vilnius Hardcore Events | [TODO: date]\n";
$cal .= "PRODID:-//YOURNAME//TOOLNAME//EN\n";

foreach ( $db as $info )
{
	$info->date = fix_date($info->date);
	$info->description = fix_description($info->description);

	$cal .= "BEGIN:VEVENT\n";
	$cal .= "DTSTART;VALUE=DATE-TIME:".$info->date."\n";
	$cal .= "DTEND;VALUE=DATE-TIME:".$info->date."\n";
	$cal .= "SUMMARY:".$info->title."\n";
	$cal .= "DESCRIPTION:".$info->description."\n";
	$cal .= "URL:http://url.com/TODO\n";
	$cal .= "UID:".$info->link."\n";
	$cal .= "END:VEVENT\n";
}

$cal .= "END:VCALENDAR\n";

//echo $cal;
echo file_put_contents ( "hardcore.ics", $cal );

?>