Products
GG网络技术分享 2025-03-18 16:12 2
I have used this following function in my localhost which is working fine
// Image cropping
function cropImage($sourcePath, $thumbSize, $destination = null) {$parts = explode(\'.\', $sourcePath);
$ext = $parts[count($parts) - 1];
if ($ext == \'jpg\' || $ext == \'jpeg\') {
$format = \'jpg\';
} else {
$format = \'png\';
}
if ($format == \'jpg\') {
$sourceImage = imagecreatefromjpeg($sourcePath);
}
if ($format == \'png\') {
$sourceImage = imagecreatefrompng($sourcePath);
}
list($srcWidth, $srcHeight) = getimagesize($sourcePath);
// calculating the part of the image to use for thumbnail
if ($srcWidth > $srcHeight) {
$y = 0;
$x = ($srcWidth - $srcHeight) / 2;
$smallestSide = $srcHeight;
} else {
$x = 0;
$y = ($srcHeight - $srcWidth) / 2;
$smallestSide = $srcWidth;
}
$destinationImage = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($destinationImage, $sourceImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide);
if ($destination == null) {
header(\'Content-Type: image/jpeg\');
if ($format == \'jpg\') {
imagejpeg($destinationImage, null, 100);
}
if ($format == \'png\') {
imagejpeg($destinationImage);
}
if ($destination = null) {
}
} else {
if ($format == \'jpg\') {
imagejpeg($destinationImage, $destination, 100);
}
if ($format == \'png\') {
imagepng($destinationImage, $destination);
}
}
}
But i am using this in my wordpress theme and it is showing some errors
����JFIF����?I��^��l�i�����76����6m�0ib�~���X��KR����J��W�9��.�7wK~��J��)�J��)�.�������A�_�]�pw��@��\'��{1M{���5���7�#X��|�w�#9W^�t�]������t}ms�l���%���W{���G��1T+8�2s���0a��3�XX� �yL��FrU��@�Y٦\'���r���{g��簪�;c�[�0r0q�q���N9��G��{?�c�w�K�ӯE�|ެ�Td2���F�?!#�G�z>ЀP�����u�ᓒ����d��A8��9\'t��=���b���
\'#\'ג}O��\\����P�S�]���o�h�Zy$|�Ns�q��Y��t8f+�O�
I have used this code for showing the image
$docimg = $doctor_img[\'url\'];if ($docimg):
?>
<?php cropImage( $docimg, 200, null); ?>
Is there anything to change in this code. So that i can get the image src instead of wired characters
here is the working example
$parts = explode(\'.\', $docimg);$ext = $parts[count($parts) - 1];
if ($ext == \'jpg\' || $ext == \'jpeg\') {
$format = \'jpg\';
} else {
$format = \'png\';
}
ob_start();
header( \\\"Content-type: image/\\\".$format );
cropImage( $docimg, 200, null);
###
I have found a solution for this,but i cant understand what will happen if png file will come.
ob_start();header( \\\"Content-type: image/jpeg\\\" );
cropImage( $docimg, 200, null);
$i = ob_get_clean();
echo \\\"<img src=\'data:image/jpeg;base64,\\\" . base64_encode( $i ).\\\"\'>\\\";
Because as you see, there is written for jpeg file only.
Demand feedback