function createGlitchImage($sourceImagePath, $glitchLevel = 1.0, $pixelateBlockSize = 10) {
if (!file_exists($sourceImagePath)) {
return false;
}
// Define cache path
$cacheDir = './cache/';
$cachePath = $cacheDir . 'glitch_' . basename($sourceImagePath);
// If cached version exists, return its path
if (file_exists($cachePath)) {
return $cachePath;
}
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0777, true); // 0777 gives full permissions
}
// Load the image
$imageInfo = getimagesize($sourceImagePath);
$width = $imageInfo[0];
$height = $imageInfo[1];
switch ($imageInfo['mime']) {
case 'image/jpeg':
$image = imagecreatefromjpeg($sourceImagePath);
break;
case 'image/png':
$image = imagecreatefrompng($sourceImagePath);
break;
default:
return false;
}
// Pixelate Effect
$pixelatedImage = imagescale($image, $width / $pixelateBlockSize, $height / $pixelateBlockSize);
$pixelatedImage = imagescale($pixelatedImage, $width, $height);
// Glitch Effect
for ($y = 0; $y < $height; $y += rand(5, 20)) {
$shift = rand(-$glitchLevel * 5, $glitchLevel * 5);
imagecopy($pixelatedImage, $pixelatedImage, $shift, $y, 0, $y, $width - abs($shift), 1);
}
// Save to cache
imagepng($pixelatedImage, $cachePath);
imagedestroy($image);
imagedestroy($pixelatedImage);
return $cachePath;
}
// Usage example
$cachedImagePath = createGlitchImage("cover.jpg", 2.0, 15);
echo "Glitched image stored at: " . $cachedImagePath . '<br>';