wordpress wp_ajax_[ACTION] display a broken image
-
I used a single .php file to display a resized image in wordpress before and it is work fine, this is my code:
if(isset($_GET['src'])){ $src = urldecode($_GET['src']); }else{ exit; } $width = 300; $height = 300; resizeImg($src,$width,$height); function resizeImg($src,$width,$height){ $arr = getimagesize($src); header("Content-type: image/png"); $ext = pathinfo($src, PATHINFO_EXTENSION); switch(strtolower($ext)){ case 'jpg': case 'jpeg': $src = imagecreatefromjpeg($src); break; case 'gif': $src = imagecreatefromgif($src); break; case 'png': $src = imagecreatefrompng($src); break; case 'wbmp': $src = imagecreatefromwbmp($src); break; default: exit; } //$src = imagecreatefromjpeg($src); $image = imagecreatetruecolor($width, $height); $bg = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bg); imagecopyresampled($image, $src, 0, 0, 0, 0, $width, $height, $arr[0], $arr[1]); imagepng($image); imagedestroy($image); }
The image link is:
https://localhost/wordpress/wp-content/plugins/wechatshare/ws-resize.php?src=http%3A%2F%2Flocalhost%2Fwordpress%2Fwp-content%2Fuploads%2F2016%2F09%2F343.jpgAnd now I use wordpress hook wp_ajax_[ACTION] to display it:
add_action( 'wp_ajax_nopriv_resize_img', 'ws_resize_img' ); add_action( 'wp_ajax_resize_img', 'ws_resize_img' ); //resize function ws_resize_img(){ if(isset($_GET['src'])){ $src = urldecode($_GET['src']); }else{ exit; } $width = 300; $height = 300; $arr = getimagesize($src); header("Content-type: image/png"); $ext = pathinfo($src, PATHINFO_EXTENSION); switch(strtolower($ext)){ case 'jpg': case 'jpeg': $src = imagecreatefromjpeg($src); break; case 'gif': $src = imagecreatefromgif($src); break; case 'png': $src = imagecreatefrompng($src); break; case 'wbmp': $src = imagecreatefromwbmp($src); break; default: exit; } //$src = imagecreatefromjpeg($src); $image = imagecreatetruecolor($width, $height); $bg = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bg); imagecopyresampled($image, $src, 0, 0, 0, 0,$width,$height,$arr[0], $arr[1]); imagepng($image); imagedestroy($image); exit; }
The image link is:
https://localhost/wordpress/wp-admin/admin-ajax.php?action=resize_img&src=http%3A%2F%2Flocalhost%2Fwordpress%2Fwp-content%2Fuploads%2F2016%2F09%2F343.jpgBut I get a broken image.
And who can tell me why?
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘wordpress wp_ajax_[ACTION] display a broken image’ is closed to new replies.