I was wondering if it was possible to manipulate data in an array that matches a particular string? But I'm honestly not sure where to begin googling or if my statement even makes sense.
I have a PHP page with an array stored in a variable ($user). By default, the array displays "Welcome to Planet Earth" followed by "Population 0" when the $user variable is called to generate an image. I also have created the ability to change the array "name" and "description" from a form on another page.
<?php
// link to the font file on the server
$fontname = 'font/HelveticaNeue-BoldCond.otf';
// controls the spacing between text
$i=30;
//JPG image quality 0-100
$quality = 90;
function create_image($user){
global $fontname;
global $quality;
$file = "covers/welcome.jpg";
// if the file already exists dont create it again just serve up the original
//if (!file_exists($file)) {
// define the base image that we lay our text on
$im = imagecreatefromjpeg("pass.jpg");
// setup the text colours
$color['blue'] = imagecolorallocate($im, 13, 42, 102);
$color['orange'] = imagecolorallocate($im, 245, 130, 32);
// this defines the starting height for the text block
$y = imagesy($im) - $height - 365;
// loop through the array and write the text
foreach ($user as $value){
// center the text in our image - returns the x value
$x = center_text($value['name'], $value['font-size']);
imagettftext($im, $value['font-size'], 0, $x, $y+$i, $color[$value['color']], $fontname,$value['name']);
// add 32px to the line height for the next text block
$i = $i+32;
}
// create the image
imagejpeg($im, $file, $quality);
//}
return $file;
}
function center_text($string, $font_size){
global $fontname;
$image_width = 800;
$dimensions = imagettfbbox($font_size, 0, $fontname, $string);
return ceil(($image_width - $dimensions[4]) / 2);
}
$user = array(
array(
'name'=> 'Welcome to Planet Earth',
'font-size'=>'27',
'color'=>'blue'),
array(
'name'=> 'Population 0',
'font-size'=>'27',
'color'=>'orange'),
);
if(isset($_POST['submit'])){
$error = array();
if(strlen($_POST['name'])==0){
$error[] = 'Please enter Planet Name';
}
if(count($error)==0){
$user = array(
array(
'name'=> $_POST['name'],
'font-size'=>'27',
'color'=>'blue'),
array(
'name'=> $_POST['description'],
'font-size'=>'16',
'color'=>'orange'),
);
}
}
// run the script to create the image
$filename = create_image($user);
?>
Now my question is: Is there any way possible to change the color of the letter "A" in "Earth" to Orange while leaving the rest of the text Blue?
Bonus: Any way to ensure that the "A" in "Planet" and/or "Population" is not changed to Orange in addition to the "A" in "Earth"?
If anyone can help point me in the right direction, that would be amazing! I'm stumped and feel like I'm using the wrong search terms to determine if this is possible or not.