Posts tagged Php
March 04th, 2010
Round image corners with php
Topics: Howto, Php, CodeI needed to round the corners of a banner image dynamically in PHP and I couldn’t find any explanations. Anyway it turns out the easiest way is to make an image of a rounded corner, like this rounded-corner.png and then stamp it on all 4 corners. You have to rotate it each time of course.Anyway here’s a snipplet of code to give you an idea:
<?php
$banner = '/path/to/banner-image.png';
$corner = '/path/to/rounded-corner.png';
// Load up the images
$banner = open_image($banner);
$corner = open_image($corner);
// Round the corner
imagecopy($banner, $corner, 0, 0, 0, 0, imagesx($corner), imagesy($corner));
$corner = imagerotate($corner, 90, 0);
imagecopy($banner, $corner, 0, imagesy($banner) - imagesy($corner), 0, 0, imagesx($corner), imagesy($corner));
$corner = imagerotate($corner, 90, 0);
imagecopy($banner, $corner, imagesx($banner) - imagesy($corner), imagesy($banner) - imagesy($corner), 0, 0, imagesx($corner), imagesy($corner));
$corner = imagerotate($corner, 90, 0);
imagecopy($banner, $corner, imagesx($banner) - imagesy($corner), 0, 0, 0, imagesx($corner), imagesy($corner));
// Output the image
header("Content-type: image/gif");
imagegif($banner);
// Tidy up
imagedestroy($banner);
imagedestroy($corner);
?>
December 17th, 2008
Quick stripping non-ascii in PHP
Topics: Code, PhpWorking on this contract required me to strip non-ascii characters out of their content before sending it to Sphinx to be indexed. “No problem” I thought, “I’ll just use that function I wrote earlier“…
Whoops, to filter some test data it took like 3 minutes! So I did some further tests, here where my findings:
- strip_utf - 3 mins and 20.546s
- str_replace($crap, ”, $string) - 3 mins 6 seconds
- preg_replace(’/[^(\x20-\x7F)]*/’, ”, $content) - 2 mins 10 seconds
- No filtering - 8 seconds!
So it still wasn’t fast enough. Finally I wrote a small chunk of C to strip the crap, piping the data through that gave:
- 12 seconds
Not bad! The code is pretty simple too, in case you want it here you go:
Wordpress has mangled the code so badly, I have given up trying to get it to display properly. View it here
Edit: Wordpress' gone so here you go:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
unsigned int Chr;
while ((Chr = getchar()) != EOF) {
if ((Chr > 31 && Chr < 128) || (Chr == 13) || (Chr == 10))
putchar(Chr);
}
return 0;
}
Next up, making a module to use some C to strip these characters from within PHP..
(Also next Wordpress is going to be replaced)
- Dave.
(Thanks to erisco in ##php for the suggestions and help.)
Edit: By the way the time of 3 minutes mattered because that meant with the entire database the time would be like 5 or 6 hours, with the speed increase I managed to get it’s down to 20 minutes :D
December 03th, 2008
Parsing a number from a string in PHP
Topics: Php, Code, HowtoAnother quick PHP snippet, a function that returns the first integer found in the string given:
// Take a string and return the first number found
function parse_int($string) {
ereg("([0-9]+)", $string, $results);
if (is_array($results))
return (int)$results[0];
else
return NULL;
}
December 02th, 2008
How to strip Unicode and UTF using PHP
Topics: Code, Php, HowtoI have been working on a PHP cotract recently and I needed to strip out anything non-asci.
Here’s a little function I wrote that takes a string and returns it minus all the crazy symbols:
// Function to strip out all non asci characters
function strip_utf($string) {
for ($i = 0; $i < strlen ($string); $i++)
if (ord($string[$i]) > 127)
$string[$i] = " ";
return $string;
}
Hope it helps, Dave.