Horje

Tips (Total 4)


# Tips-1) How to check Image error or valid in PHP

Use getimagesize() it will return FALSE if file corrupt otherwise if file is ok then it will return array containing file info like mime, height and width etc, try below example

Check Image is Valid

Here runs following code to Check Image is Valid
index.php
Example: PHP
<?php
  $fileName = 'https://horje.com/avatar.png'; //filepath
  if(getimagesize($fileName) === false){
    echo "Image is corrupted";
  }
  else{
    echo "Image is ok";
  }
?>

Output should be:

Check Image is Valid

Check Image is corrupted

Here runs following code to Check Image is corrupted
index.php
Example: PHP
<?php
  $fileName = 'https://horje.com/efeff.png'; //filepath
  if(getimagesize($fileName) === false){
    echo "Image is corrupted";
  }
  else{
    echo "Image is ok";
  }
?>

Output should be:

Check Image is corrupted

# Tips-2) How to create Title to Image in PHP

Here is simplae example of Title to Image in PHP

Full Completed Code

index.php
Example: PHP
<?php
$title = 'Bangladesh is a Country';
$link = 'horje.com';
$cat = 'Blog Category';
$date = '05-Jan-2024';
$web = '[www.horje.com]';
$str1= $cat; 
$str2= $link;
$str3= $title;
$str4= $date;
$str5= $web;
?>
<?php
   
// Create the size of image or blank image
$image = imagecreate(350, 250);
   
// Set the background color of image
$background_color = imagecolorallocate($image,70,130,180);
   
// Set the text color of image
$text_color = imagecolorallocate($image, 255, 255, 255);

// Function to create image which contains string.
imagestring($image, 5, 45, 80, $str1, $text_color);
imagestring($image, 3, 45, 100, $str3, $text_color);
imagestring($image, 3, 45, 120, $str2, $text_color);
imagestring($image, 3, 45, 140, $str4, $text_color);
imagestring($image, 3, 45, 160, $str5, $text_color);



   
header("Content-Type: image/png");
   
imagepng($image);
imagedestroy($image);
?>

Output should be:

Full Completed Code

# Tips-3) How to convert a image link to Base64 in PHP

I think that it should be.

FUll Code

See the Example.
index.php
Example: PHP
<?php
function getDataURI($imagePath) {
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $type = $finfo->file($imagePath);
    return 'data:' . $type . ';base64,' . base64_encode(file_get_contents($imagePath));
}

// Use the above function like below:
echo '<img src="' . getDataURI('https://horje.com/avatar.png') . '" alt="">';
echo '<img src="' . getDataURI('https://horje.com/avatar.png') . '" alt="">';

?>

Output should be:

Demo

Output should be:


# Tips-4) How to convert all external image link from html string to base64 in PHP

See the example.

Full Code Example

Learn the Example.
index.php
Example: PHP
<?php
$html = '            <div class="itemer">
<span class="pointer" onclick="z49()">   
<center>
</br>    
<img src="https://website.sitelop.com/small/amazon.cn/icon.jpg" alt="Amazon.com. Spend less. Smile more." style="width:36px;height:36px; border-radius: 20%;" >
amazon.cn</center>     
</span> 
</div>

            <div class="itemer">
<span class="pointer" onclick="z50()">   
<center>
</br>    
<img src="https://website.sitelop.com/small/cnn.com/icon.jpg" alt="Breaking News, Latest News and Videos | CNN" style="width:36px;height:36px; border-radius: 20%;" >
cnn.com</center>     
</span> 
</div>';
$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
        $src = $image->getAttribute('src');
        $type = pathinfo($src, PATHINFO_EXTENSION);
        $data = file_get_contents($src);
        $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
        $image->setAttribute("src", $base64); 
}
$html = $dom->saveHTML();

echo $html;
?>

Output should be:

Demo

Output should be: